0

I'm trying to get substring from string tekst, after pressing button the app is crashing. This is code of onClick, the rest of code is fine, when im doing setText() it setting it fine but when i'm trying some kind of tekst.substring() or indexof it crashes.

public void otworz(View view) throws Exception {

    new JSONTask().execute("http://www.filmweb.pl/serial/Biuro-2005-202887");

    String tekst1 = new String(tekst.substring(10,20));
    tView.setText(tekst1);
}

In logcat, I am getting this:

10-13 19:43:00.321 11118-11118/com.kuba.zneta E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.substring(int, int)' on a null object reference
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
KubenQPL
  • 113
  • 2
  • 11
  • 2
    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 – CommonsWare Oct 13 '15 at 17:51
  • What is the value of "tekst"? – aryn.galadar Oct 13 '15 at 17:53
  • check the length of tekst and check if it's null or empty. – Ritt Oct 13 '15 at 17:56
  • The error is "10-13 19:43:00.321 11118-11118/com.kuba.zneta E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.substring(int, int)' on a null object reference " – KubenQPL Oct 13 '15 at 18:12
  • @KubenQPL Well, that means `tekst` is equal to `null`. Now you have to find out why it happens and how to change that. – Gumbo Oct 13 '15 at 18:16
  • but it can't be null because when use method setText(tekst) String from tekst appear in textView – KubenQPL Oct 13 '15 at 18:32
  • When are you using the method `setText(teskst)`? I ask because that error is definitely saying `teskst == null` when you attempt `tesk.substring(10, 20)` – zgc7009 Oct 13 '15 at 20:24
  • You left out the part of the code that initializes tesk, so all we can do is guess what you meant to do. – leonardkraemer Oct 14 '15 at 08:14

2 Answers2

0

You should put this code String tekst1 = new String(tekst.substring(10,20)); tView.setText(tekst1); to onPostExecute function in asycntask.

alican akyol
  • 303
  • 3
  • 14
0

You have to check size of tekst first then set textview.Explained below

   if( teskst.length()>=120)
       {
          String str = new String(tekst.substring(100,120));

          textview.setText(str);

       }

       else

       textview.setText(str);
Rahul Chaudhary
  • 1,059
  • 1
  • 6
  • 15