0

I am making an app to display data from a database into a list view. When an item on the list view is clicked, it takes the user to a new activity where they can view more details about that item. I want to make the details page dynamic to display the details and have managed to show the title of the list view item in a toast.

Now, I am trying to display this by using setText() to show the title in a string but am getting the error:

AndroidRuntime: FATAL EXCEPTION: main
         Process: com.example.kathe.parenttripapp, PID: 22849
                                                                             java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.kathe.parenttripapp/com.example.kathe.parenttripapp.Details}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.content.Intent.getSerializableExtra(java.lang.String)' on a null object reference
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                                                 at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:135)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.content.Intent.getSerializableExtra(java.lang.String)' on a null object reference
                                                                                 at com.example.kathe.parenttripapp.Details.<init>(Details.java:23)
                                                                                 at java.lang.reflect.Constructor.newInstance(Native Method)
                                                                                 at java.lang.Class.newInstance(Class.java:1606)
                                                                                 at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                                                 at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                 at android.os.Looper.loop(Looper.java:135) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

this is the class the error occurs at:

TextView titletext;
final List<Activitytable> activityTable = Activitytable.listAll(Activitytable.class);

String data = getIntent().getSerializableExtra("listPosition").toString();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details);

    TextView titletext = (TextView) findViewById(R.id.titletext);

    String data = getIntent().getSerializableExtra("listPosition").toString();

    Toast.makeText(getBaseContext(),String.valueOf(data),Toast.LENGTH_LONG).show();

    setData();
}

private void setData() {
    Intent i = getIntent();
    Bundle b = i.getExtras();
    if (data != null) {
        String j = (String) b.get("listPosition");
        titletext.setText(j);
    }
    else{
        titletext.setText("Hello");
    }
}

This is the activity it has come from:

final ListView listView = (ListView) findViewById(R.id.viewAll_listview);
    long count = Activitytable.count(Activitytable.class);

    if(count>0) {
        final List<Activitytable> activitytable = Activitytable.listAll(Activitytable.class);
        final ViewAllListView madapter = new ViewAllListView(getApplicationContext(), activitytable);
        listView.setAdapter(madapter);
        listView.setClickable(true);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?>parent, View v, int position, long id) {
                String title = activitytable.get(position).Title.toString();
                Activitytable AT = Activitytable.findById(Activitytable.class,activitytable.get(position).getId());
                Intent i = new Intent(getApplicationContext(),Details.class);
                i.putExtra("listPosition",title);
                startActivity(i);

            }
            public Object getItem(int position) {return position;}
        });

    }
        else

            {
                Toast.makeText(getApplicationContext(), "No Data Available in Table", Toast.LENGTH_LONG);
            }
        }

What I would like to do is to put the intent data into the TextView 'titletext' and then do an if statement saying if the passed intent data is equal to an activity title then display the following data but can't work out what is going wrong. I have tried using getStringExtra() instead of getSerializableExtra but no such luck. Works on toast but not on TextView.

kjones
  • 3
  • 2
  • @sihao Can you help? – kjones Apr 05 '16 at 19:55
  • This question about NullPointerExceptions has been asked numerous times: http://stackoverflow.com/q/218384/1478764. Please do a little research before asking a question. – chRyNaN Apr 05 '16 at 19:56
  • You don't have an Intent, therefore `getIntent()` is null. – OneCricketeer Apr 05 '16 at 19:57
  • @cricket_007 my intent is on the activity it came from isn't it? Surely it wouldn't even be able to start the activity if I didn't have an intent but it starts fine without the setData() method – kjones Apr 05 '16 at 20:01
  • 2
    You called `getIntent()` outside of `onCreate`. That's the problem. The intent doesn't exist at that point – OneCricketeer Apr 05 '16 at 20:03

3 Answers3

1

If you don't have some good understanding of why you are doing so, try not to initialize your variables until you are in onCreate, also to prevent a NullPointerException, it is a good habit to use if (variable != null).

And you are storing an int, so use getIntExtra

TextView titletext;
List<Activitytable> activityTable;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details);

    activityTable = Activitytable.listAll(Activitytable.class);
    titletext = (TextView) findViewById(R.id.titletext);

    Intent i = getIntent();
    if (i != null) {
        String data = i.getStingExtra("listPosition");
        titletext.setText(String.valueOf(data));
    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • You may also want to point out that `getSerializableExtra` can return null as well. So, a call to `toString` may also cause a NullPointerException. – chRyNaN Apr 05 '16 at 20:07
  • @chRyNaN I changed to `getIntExtra`, but fixed it. – OneCricketeer Apr 05 '16 at 20:10
  • It doesn't like the ("listPosition") - it might be good to point out that listPosition is supposed to display text not integer if that helps? **Edit** Changed it to getStringExtra("listPosition") but still the same runtime error (nullPointerException) – kjones Apr 05 '16 at 20:19
  • You have to get the same type of data that you put in, otherwise you get null. I have edited with the correct usage – OneCricketeer Apr 05 '16 at 20:27
  • I am trying to display text, not integer, I am using data = String.valueOf(i.getStringExtra("listPosition")); titletext.setText(data); but it still doesn't like it – kjones Apr 05 '16 at 20:30
  • I didn't think I had - on the initial intent I put the String 'title' as the extra info. The program is definitely passing a piece of text (the title of the item) because it displays in a Toast perfectly fine. I just want to put the same text that is in the toast onto the textview - sorry if my question was not completely clear – kjones Apr 05 '16 at 20:45
  • I've edited my question, I really can't apologize enough - I've been working on this project for days straight without many breaks and I had copied this code from an older version of my code. I have now edited my question to show the newest version of my code - it shows the current intent extras I want to display. Basically I'm an idiot – kjones Apr 05 '16 at 20:54
  • Well, I still feel like my answer should have worked given the code you initially provided. – OneCricketeer Apr 05 '16 at 20:55
  • Now in my edited code in my question, I have a string being passed and it works in the toast but not in the string 'titletext' – kjones Apr 05 '16 at 20:57
  • The runtime error is still occuring when I open the activity and the error is said to occur at this line: titletext.setText(data); I literally do not understand why this is happening - my code is the exact same as your answer – kjones Apr 05 '16 at 21:07
  • Thanks for all your help, I realized my mistake was in my copying of your code, I am very sleep deprived - not that this is an excuse. Thanks again, you were a massive help to me – kjones Apr 05 '16 at 21:18
0

In onCreate() and don't do it as a member variable.

Bundle intentBundle = getIntent().getExtras();
if (intentBundle != null) {
        lastPosition = getInt( "lastPostion" );
}
Floern
  • 33,559
  • 24
  • 104
  • 119
mjstam
  • 1,049
  • 1
  • 6
  • 5
-1

The null pointer you are receiving is in the Details class on line 23

 at com.example.kathe.parenttripapp.Details.<init>(Details.java:23)