Reputation: 1
so I got this JSON:
{ "games": [
{
"location": "germany",
"team1": {
"name": "test1",
"tore": "2"
},
"team2": {
"name": "test2",
"tore": "3"
}
},...
My Model Class for this JSON looks like that(where I think i got the error)
MODEL CLASS:
import com.google.gson.annotations.SerializedName;
public class GameModel {
public String location;
@SerializedName("team1")
public Team team1;
@SerializedName("team2")
public Team team2;
public String getLocation() {
return zeitpunkt;
}
public void setLocation(String location) {
this.location= location;
}
class Team
public String name;
public String tore;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTore() {
return tore;
}
public void setTore(String tore) {
this.tore = tore;
}
@Override
public String toString() {
return "Team{" +
name='" + name + '\'' +
", tore='" + tore + '\'' +
'}';
}
}
And my GSON looks like that..
GSON Class
JSONObject parentObject = new JSONObject(finalJson);
JSONArray parentArray = parentObject.getJSONArray("games");
gameModelList = new ArrayList<>();
Gson gson2 = new Gson();
for(int i=0; i<parentArray.length(); i++) {
JSONObject finalObject = parentArray.getJSONObject(i);
GameModel inputGame = gson2.fromJson(finalObject.toString(), GameModel.class);
gameModelList.add(inputGame);
}
return gameModelList.toString();
My location from games
works that i call in my CustomAdapter Class, but everytime I want to get the name of team1 like
GameModel gm = getItem(position);
tvTeamOne.setText("" +gm.team1.getName());
I get a java.null.lang pointer exception
I also tried the debugger, and it displayed me team1:null
, so he doesn't get the String
logcat:
07-04 13:26:53.720 25600-25600/de.xxxxxxx.app.mc.xxxxxx E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at de.xxxxx.app.mc.xxxxx.GameAdapter.getView(GameAdapter.java:40)
at android.widget.AbsListView.obtainView(AbsListView.java)
at android.widget.ListView.measureHeightOfChildren(ListView.java)
at android.widget.ListView.onMeasure(ListView.java)
at android.view.View.measure(View.java)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java)
at android.widget.LinearLayout.measureVertical(LinearLayout.java)
at android.widget.LinearLayout.onMeasure(LinearLayout.java)
at android.view.View.measure(View.java)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java)
at android.view.View.measure(View.java)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
at android.widget.FrameLayout.onMeasure(FrameLayout.java)
at android.view.View.measure(View.java)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java)
at android.widget.LinearLayout.measureVertical(LinearLayout.java)
at android.widget.LinearLayout.onMeasure(LinearLayout.java)
at android.view.View.measure(View.java)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
at android.widget.FrameLayout.onMeasure(FrameLayout.java)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java)
at android.view.View.measure(View.java)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java)
at android.view.Choreographer.doCallbacks(Choreographer.java)
at android.view.Choreographer.doFrame(Choreographer.java)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java)
at android.os.Handler.handleCallback(Handler.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 881
Reputation: 3994
All the code here is irrelevant .Only the "GameAdapter" getview is required.Also I think since you are calling "getItem(position)"
you need to change this method like this.
public GameModel getItem(int position) {
return gameModelList.get(position);
}
Hope this helps.
Upvotes: 0
Reputation: 2606
first create models:
public class Team1
{
public String name;
public String tore;
}
public class Team2
{
public String name;
public String tore;
}
public class Game
{
public String location;
public Team1 team1;
public Team2 team2;
}
public class YourRootObject
{
public List<Game> games;
}
then simple parse to object:
Gson gson = new Gson();
try {
YourRootObject result = (YourRootObject) gson.fromJson(yourJson, YourRootObject.class);
catch(Exception e){}
and in result all data if it exists, will be proper parsed and you will get no null in values
Upvotes: 2