我有空指针异常的问题。 2天我什么也做不了。请帮帮我。 NullPointer在线。这是一个问题,但我不知道该怎么做。
for (AsanaPOJO a : asanas.getAsanas()) {
Log.d("AsanaPROGRAM LOGS", BuildConfig.FLAVOR + a.getName());
}
这是JSON:
{
"asanas":
[
{
"id": 34,
"name": "Easy Pranayama",
"imgLarge": "/system/photos/asanas/34/large/34.jpg",
"imgThumb": "/system/photos/asanas/34/thumb/34.jpg",
"level_id": 1,
"type_id": 9,
"audio": "/system/audios/asanas/34/body/en/34.mp3",
"title_audio": "/system/audios/asanas/34/title/en/34.mp3",
"duration": 69,
"fullDesc": "Lie down comfortably on your back so that your abdominal muscles are relaxed. Close your eyes.",
"photo": "/system/photos/asanas/34/original/34.jpg",
"sanskrit_name": "Sahaj Pranayama ",
"sanskrit_audio_title": "/sanskrit_audio_titles/original/missing.png",
"breathing_video": "/system/videos/asanas/34/breathing/34.mp4",
"is_paid": false,
"is_breathing": true,
"old_identifier": "331",
"old_pk": 772
},
{
"id": 35,
"name": "Easy Pranayama Advanced",
"imgLarge": "/system/photos/asanas/35/large/35.jpg",
"imgThumb": "/system/photos/asanas/35/thumb/35.jpg",
"level_id": 2,
"type_id": 9,
"audio": "/system/audios/asanas/35/body/en/35.mp3",
"title_audio": "/system/audios/asanas/35/title/en/35.mp3",
"duration": 92,
"fullDesc": "Lie down comfortably on your back so that your abdominal muscles are relaxed. Close your eyes. Bring your hands behind your head.",
"photo": "/system/photos/asanas/35/original/35.jpg",
"sanskrit_name": "Sahaj Pranayama Advanced",
"sanskrit_audio_title": "/sanskrit_audio_titles/original/missing.png",
"breathing_video": "/system/videos/asanas/35/breathing/35.mp4",
"is_paid": true,
"is_breathing": true,
"old_identifier": "332",
"old_pk": 773
}
]
}
这是主要活动:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MYApi myapi = (MYApi) new Builder().setLogLevel(LogLevel.FULL).setEndpoint(MYurl.BASE_URL).build().create(MYApi.class);
myapi.getAsanas(new Callback<Asanas>() {
public void success(Asanas asanas,Response response) {
for (AsanaPOJO a : asanas.getAsanas()) {
Log.d("AsanaPROGRAM LOGS", BuildConfig.FLAVOR + a.getName());
}
}
public void failure(RetrofitError error) {
}
});
}
班级体式:
public class Asanas {
private List<AsanaPOJO> asanaPOJOs;
public Asanas() {
}
public List<AsanaPOJO> getAsanas ()
{
return this.asanaPOJOs;
}
public void setAsanas (List<AsanaPOJO> asanas)
{
this.asanaPOJOs = asanaPOJOs;
}
@Override
public String toString()
{
return "ClassPojo [asanas = "+asanaPOJOs+"]";
}
}
Class AsanaPOJO
public class AsanaPOJO {
private int id;
private String name;
private String imgLarge;
private String imgThumb;
private int level_id;
private int type_id;
private String audio;
private String title_audio;
private int duration;
private String fullDesc;
private String photo;
private String sanskrit_name;
private String sanskrit_audio_title;
private String breathing_video;
private String is_paid;
private String is_breathing;
private int old_identifier;
private int old_pk;
public AsanaPOJO() {
}
public String getSanskrit_audio_title ()
{
return sanskrit_audio_title;
}
public void setSanskrit_audio_title (String sanskrit_audio_title)
{
this.sanskrit_audio_title = sanskrit_audio_title;
}
public String getAudio ()
{
return audio;
}
public void setAudio (String audio)
{
this.audio = audio;
}
public String getImgThumb ()
{
return imgThumb;
}
public void setImgThumb (String imgThumb)
{
this.imgThumb = imgThumb;
}
public String getFullDesc ()
{
return fullDesc;
}
public void setFullDesc (String fullDesc)
{
this.fullDesc = fullDesc;
}
public String getImgLarge ()
{
return imgLarge;
}
public void setImgLarge (String imgLarge)
{
this.imgLarge = imgLarge;
}
public int getLevel_id ()
{
return level_id;
}
public void setLevel_id (int level_id)
{
this.level_id = level_id;
}
public String getPhoto ()
{
return photo;
}
public void setPhoto (String photo)
{
this.photo = photo;
}
public int getType_id ()
{
return type_id;
}
public void setType_id (int type_id)
{
this.type_id = type_id;
}
public int getOld_pk ()
{
return old_pk;
}
public void setOld_pk (int old_pk)
{
this.old_pk = old_pk;
}
public int getId ()
{
return id;
}
public void setId (int id)
{
this.id = id;
}
public int getDuration ()
{
return duration;
}
public void setDuration (int duration)
{
this.duration = duration;
}
public String getTitle_audio ()
{
return title_audio;
}
public void setTitle_audio (String title_audio)
{
this.title_audio = title_audio;
}
public String getIs_breathing ()
{
return is_breathing;
}
public void setIs_breathing (String is_breathing)
{
this.is_breathing = is_breathing;
}
public String getName ()
{
return name;
}
public void setName (String name)
{
this.name = name;
}
public int getOld_identifier ()
{
return old_identifier;
}
public void setOld_identifier (int old_identifier)
{
this.old_identifier = old_identifier;
}
public String getIs_paid ()
{
return is_paid;
}
public void setIs_paid (String is_paid)
{
this.is_paid = is_paid;
}
public String getSanskrit_name ()
{
return sanskrit_name;
}
public void setSanskrit_name (String sanskrit_name)
{
this.sanskrit_name = sanskrit_name;
}
public String getBreathing_video ()
{
return breathing_video;
}
public void setBreathing_video (String breathing_video)
{
this.breathing_video = breathing_video;
}
@Override
public String toString(){
return "Asana"+name+","+"id";
}
public int checkBool(String s){
if (s.equals("true")){
return 1;
}
else return 0;
}
}
改造界面
public interface MYApi {
@GET("/asanas")
void getAsanas(Callback<Asanas> response);
}
和日志:
"is_paid":true,"is_breathing":false,"old_identifier":"248","old_pk":1044}]}
09-24 12:06:16.049 20971-20998/com.example.user.asanasproject D/Retrofit﹕ <--- END HTTP (261559-byte body)
09-24 12:06:16.539 20971-20971/com.example.user.asanasproject D/AndroidRuntime﹕ Shutting down VM
09-24 12:06:16.539 20971-20971/com.example.user.asanasproject W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40c0aa68)
09-24 12:06:16.539 20971-20971/com.example.user.asanasproject E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.user.asanasproject.MainActivity$1.success(MainActivity.java:37)
at com.example.user.asanasproject.MainActivity$1.success(MainActivity.java:27)
at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4517)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
通过查看您的JSON响应,字段el_GR
和is_paid
是布尔值,但在您的班级is_breathing
中,您将其声明为AsanaPOJO
。
尝试将它们更改为布尔值。
修改强>
同样@NaviRamyle说 - 您的String
类ArrayList变量名称与JSON响应不匹配。
答案 1 :(得分:1)
将private List<AsanaPOJO> asanaPOJOs;
更改为private List<AsanaPOJO> asanas;
模型变量名称必须与JSON密钥名称完全相同
{
"asanas": [..]
}