0

sorry in advance if I come across annoyed at this point! I've been trying for multiple hours to fix this :)

So, I really don't understand java NPEs. I should be getting the intent extras and I don't.

In MainActivity.java

...
Intent i = new Intent(MainActivity.this,scriptViewer.class);
i.putExtra("script_name",script_name)
startActivity(i)

So far, it is ok, no?

Intent j = getIntent();
String script_name = j.getExtras().getString("script_name");

The program has problems with this -

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference

++ Trust me, I've looked for ages, but nothing has seemed to work - TIA

  • 3
    `i.getExtras()` or `j.getExtras()` ? – Gabriele Mariotti Sep 02 '19 at 20:46
  • If you don't understand NPEs I would strongly recommend [you read this](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) for future debugging. – Mihir Kekkar Sep 02 '19 at 20:59
  • @GabrieleMariotti i changed it. It was just a typo on this post only. –  Sep 02 '19 at 21:24
  • @MihirKekkar that answer involves Integer and not String. –  Sep 02 '19 at 21:26
  • @HenryHudson In your code `j` is `null`. – Gabriele Mariotti Sep 02 '19 at 21:28
  • @GabrieleMariotti I don't understand - I recieved the Intent... –  Sep 02 '19 at 21:48
  • 2
    You might consider editing your question and posting the entire class that contains the code that is failing. Depending on where those lines reside, they may or may not work. – CommonsWare Sep 02 '19 at 22:39
  • Please take a look at here. It seems the same problem. https://stackoverflow.com/questions/31069017/null-value-in-getintent-getextras-getstring – Suman Kumar Dash Sep 03 '19 at 00:00
  • Please take a look at here. It seems the same problem. [https://stackoverflow.com/questions/31069017/null-value-in-getintent-getextras-getstring] – Suman Kumar Dash Sep 03 '19 at 00:05
  • @SumanDash thanks, but I've added that code in perfectly and still doesnt work...- –  Sep 03 '19 at 00:37
  • Please post the code for scriptViewer.java (should be ScriptViewer btw) and your AndroidManifest.xml – alexbtr Sep 03 '19 at 03:08
  • @HenryHudson the linked question on NPEs uses the `Integer` class as an example, you can replace the `Integer` variable and initialization with a `String` variable and initialization and the actual reasoning behind the NPE would still apply (the fact that NPEs stem from reference variables containing null values). – Mihir Kekkar Sep 03 '19 at 05:31

0 Answers0