-1

I'm trying to get some data from a xml document using XmlPullParser. When i'm trying to run the application is crushing due to a null object reference. The code and the error are below, but i don't see any problem with the code.

 public List<Tara> parseXML(Context context){
        try{
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();

            InputStream stream = context.getResources().openRawResource(R.raw.data_info);
            xpp.setInput(stream, null);

            int eventType = xpp.getEventType();
            while( eventType != XmlPullParser.END_TAG){
                if(eventType == XmlPullParser.START_TAG){
                    handleStartTag(xpp.getName());
                } else if (eventType == XmlPullParser.END_TAG){
                    tagCurent = null;
                } else if(eventType == XmlPullParser.TEXT){
                    handleText(xpp.getText());
                }

                eventType = xpp.next();
            }


        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return listaTara;
    }

It seems that on the line

InputStream stream = context.getResources().openRawResource(R.raw.data_info);

the next error is displayed:

01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: FATAL EXCEPTION: main
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: Process: com.example.ionut.simi_word_info, PID: 7539
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ionut.simi_word_info/com.example.ionut.simi_word_info.views.allCountry_activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2546)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at android.app.ActivityThread.access$900(ActivityThread.java:177)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5942)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at android.content.ContextWrapper.getResources(ContextWrapper.java:90)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:74)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at com.example.ionut.simi_word_info.helps.CountryPullParser.parseXML(CountryPullParser.java:31)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at com.example.ionut.simi_word_info.views.allCountry_activity.<init>(allCountry_activity.java:35)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at java.lang.reflect.Constructor.newInstance(Native Method)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at java.lang.Class.newInstance(Class.java:1650)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2536)
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at android.app.ActivityThread.access$900(ActivityThread.java:177) 
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145) 
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5942) 
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388) 
01-10 15:47:45.682 7539-7539/com.example.ionut.simi_word_info E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183) 

Thanks for the help!

Ionut Harcau
  • 11
  • 1
  • 1

1 Answers1

0

at com.example.ionut.simi_word_info.views.allCountry_activity.< init >(allCountry_activity.java:35)

Do not do anything with a Context in the Activity constructor. Do it in onCreate(Bundle) after the activity has been initialized by system.

In fact avoid overriding activity constructor, it has seldom meaning.

Eugen Pechanec
  • 34,848
  • 7
  • 94
  • 115