3

My code was working perfectly before but now it is throwing out an exception,

The code :

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
            JsonElement jsonElement = JsonParser.parseString(resultJsonString);

and the exception in the line JsonElement jsonElement = JsonParser.parseString(resultJsonString);

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method parseString(String) is undefined for the type JsonParser

Any hints to why this is happening? It was working fine till yesterday!

SharadxDutta
  • 1,058
  • 8
  • 21
  • Can you add imports? – Glains Aug 27 '20 at 07:52
  • These are the imports, import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; – SharadxDutta Aug 27 '20 at 08:04

1 Answers1

2

This looks more like you've named your own class the same way as the Gson JsonParser class. So the compiler resolves JsonParser to your own class (your.package.JsonParser) and not to the Gson JsonParser (com.google.gson.JsonParser). You may want to rename your class or use com.google.gson.JsonParser instead of JsonParser each time you want to refer to this class.

Another possibility is that the Gson library that you're using might be older than version 2.8.6. If this is the case, you've to upgrade your Gson lib. The static method JsonParser.parseString is added in version 2.8.6. Checkout the change log.

For Maven, use:

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.6</version>
</dependency>

For Gradle, use:

compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6'

For SBT, use:

libraryDependencies += "com.google.code.gson" % "gson" % "2.8.6"

For Ivy, use:

<dependency org="com.google.code.gson" name="gson" rev="2.8.6"/>

For Grape, use:

@Grapes(
    @Grab(group='com.google.code.gson', module='gson', version='2.8.6')
)

For Leiningen, use:

[com.google.code.gson/gson "2.8.6"]

For Buildr, use:

'com.google.code.gson:gson:jar:2.8.6'
1218985
  • 7,531
  • 2
  • 25
  • 31