0
public class MainActivity extends AppCompatActivity {

public String[] epc;
public int i = 0;

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



    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

public void multipleRead(View view)
{
    try {
        RFIDReader reader = RFID.open();

        reader.inventory(new RFIDCallback() {
                             @Override
                             public void onTagRead(Tag tag) {
                                 epc[i] = tag.getEPC();
                                 i++;
                             }
                         }
        );

    }
    catch (ReaderException e) {
        Toast.makeText(this, "Error: " + e, Toast.LENGTH_LONG).show();
    }
}
}

I am making a simple application that reads the ID from an RFID tag ad then displays it on the screen. The reader.inventory is called to scan multiple tags at once. I then store these tags ID number in a string array epc. The method multipleRead() is attached to a scan button on the application. So everytime the button is pressed, the multipleRead() is called.

However after I load the apk on the device and click on the button I get the following error:

E/AndroidRuntime: FATAL EXCEPTION: Thread-325
                  Process: com.example.user.myapplication, PID: 3484
                  java.lang.NullPointerException
                      at com.example.user.myapplication.MainActivity$1.onTagRead(MainActivity.java:79)
                      at com.alien.rfid.a$1.run(Unknown Source)
                      at java.lang.Thread.run(Thread.java:841)

I have read other answers similar to this but it as not helped in solving this issue. How do I get rid on the runtime error?

Updated code and error:

public class MainActivity extends AppCompatActivity {

  private List<String> epc = new ArrayList<>();
  private TextView textView;
  @Override

  public void multipleRead(View view)
  {
    try {
    // Get global RFID Reader instance
        RFIDReader reader = RFID.open();

        reader.inventory(new RFIDCallback() {
                             @Override
                             public void onTagRead(Tag tag) {
                                 epc.add(tag.getEPC());
                                 textView.append(tag.getEPC() + "\n");
                             }
                         }
        );

    }
    catch (ReaderException e) {
        Toast.makeText(this, "Error: " + e, Toast.LENGTH_LONG).show();
    }
  }

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

    textView = (TextView)findViewById(R.id.tag_id);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
  }
}

Error

E/AndroidRuntime: FATAL EXCEPTION: Thread-347
Process: com.example.user.myapplication, PID: 25039
         android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

         at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6669)
         at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1005)
         at android.view.ViewGroup.invalidateChild(ViewGroup.java:4548)
         at android.view.View.invalidate(View.java:11095)
         at android.view.View.invalidate(View.java:11044)
         at android.widget.TextView.checkForRelayout(TextView.java:6760)
         at android.widget.TextView.setText(TextView.java:3850)
         at android.widget.TextView.setText(TextView.java:3708)
         at android.widget.TextView.append(TextView.java:3443)
         at android.widget.TextView.append(TextView.java:3433)
         at com.example.user.myapplication.MainActivity$1.onTagRead(MainActivity.java:80)
         at com.alien.rfid.a$1.run(Unknown Source)
         at java.lang.Thread.run(Thread.java:841)
  • I don't know if that `Tag` parameter in `onTagRead()` can be null, but `epc` is, at least, because you never assign it. It seems like you want to be using a `List`, anyway. For example, `private List epc = new ArrayList<>();`, and then `epc.add(tag.getEPC());` in `onTagRead()`. You don't need the `int i` field to track the index, then. – Mike M. Jul 20 '20 at 00:51
  • Please initialise the epc array first in the onCreate method, then try to run again. If the issue still persists, share the full code, where the RFID reader class is initialised. – Ankit Gupta Jul 20 '20 at 00:54
  • @MikeM. it seems to working with your correction, but to confirm I then need to display this data on my screen. How do I then do this? If this works can you also post this as an answer? – Derick Brown Jul 20 '20 at 01:03
  • Well, that might need to be a new question. We do one specific issue per post. I might be able to help quickly in comments here, though. How do you mean to display this? Do you have some `View` already in your layout where this should go; like a `TextView` or `ListView`? – Mike M. Jul 20 '20 at 01:07
  • @MikeM. I got a textview "TextView textView = (TextView)findViewById(R.id.tag_id); textView.setText();" – Derick Brown Jul 20 '20 at 01:10
  • OK, if you want to add those tags to the `TextView` as they come in, you could do `private TextView textView;` at the top, `textView = (TextView) findViewById(R.id.tag_id);` in `onCreate()` (no `TextView` at the beginning of that line), and `textView.append(tag.getEPC() + "\n");` in `onTagRead()`. – Mike M. Jul 20 '20 at 01:19
  • @MikeM. app is crushing. Will post the error in a new question – Derick Brown Jul 20 '20 at 01:25
  • @MikeM. I cant post a new question until 90 min. I have included the updated code and error. Could you assit? – Derick Brown Jul 20 '20 at 01:39
  • Change the `append()` line to `runOnUiThread(new Runnable() { public void run() { textView.append(tag.getEPC() + "\n"); }});`. – Mike M. Jul 20 '20 at 01:40
  • @MikeM. I adjusted the code a bit and it works! Can you post your first comment as an answer so I can accept it? – Derick Brown Jul 20 '20 at 01:50
  • Cool. This question has already been closed as a duplicate, however, so answers can't be posted any longer. These are pretty common issues that have been addressed here before, so no need to repeat them again. Thank you, though. I appreciate the offer. Glad you got it working. Cheers! – Mike M. Jul 20 '20 at 01:52

0 Answers0