1

I am getting string from a php file at server side using retrofit 1.9 in android M like this -

...
String output = "";    
try {         
           BufferedReader reader = new BufferedReader(new InputStreamReader(result.getBody().in()));
           output = reader.readLine();
    } 
catch (IOException e) {
           ...
    }
    });
    }
    arr = output.split("-");
    textviewvar.setText(arr[0]);
...

I have tested that the output string contains HelloWorld-2017 and when I displayed individual array element arr[0] and arr[1] using Toast, they were displayed as expected. But when I am trying to setText(arr[0]), the app crashes!!! I know that the array element is not empty as I have checked them everytime. What should I do?

Edit: I tried setContentView(R.layout.activity_main) but the app continues to crash.

In the method protected void onCreate(Bundle savedInstanceState) I have initialized textviewvar whereas the variable has class scope.

  • Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this Make sure that you are logging any exceptions that you `catch`, using `Log.e()` or similar calls. – CommonsWare Jun 20 '17 at 15:24

1 Answers1

0

Posting your log cat would be useful, it will show where the crash occurred and what exception is raised.

I would expect though that textviewvar isn't initialised correctly.

In your xml you should have something like the following to declare your edittext

<EditText
android:id="@+id/my_text_view
android:width="match_parent"
android:height="wrap_content" />

In your onCreate method after you have call setContentView() you should then initialise your EditText to reference the id within your XML.

EditText textviewvar = (EditText)findViewById(R.id.my_text_view);
textviewvar.setText(arr[0]);

Check that you are doing this, and that once this is done, your textviewvar is not null, if it is, it might be that your EditText you are giving the ID for, is not in the XML layout file, that you are referencing within your setContentView call.

Boardy
  • 35,417
  • 104
  • 256
  • 447
  • In my logcat file, it is showing error as java.lang.NullPointerException: Attempt to read from null array But I think the data is being entered into the array. –  Jun 20 '17 at 16:20
  • That sound like your array isn't getting initialised, I think you will need to post how you are getting the data into the array, along with the logcat output for anyone to help any further – Boardy Jun 20 '17 at 16:22