Quantcast

Jump to content

» «
Photo

[help] Java

4 replies to this topic
Gian_Yagami
  • Gian_Yagami

    Mark Chump

  • Members
  • Joined: 08 Nov 2011
  • None

#1

Posted A week ago Edited by Gian_Yagami, A week ago.

Here my piece of code:

try{
   int i = 0; String s = null;
   while(i<=cmbBox.getItemCount()){
      if(table.getValueAt(table.getSelectedRow(), 1).equals(cmbBox.getItemAt(i).toString())){
         s = cmbBox.getItemAt(i).toString();
         cmbBox.RemoveAllItems();
         cmbBox.addItem(s);
         break;
   ​   }
​      else i++;
   }
​} catch (Exception e){
   JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.ERROR_MESSAGE);
}

 

This program runs correctly but, it is catch error "java.lang.NullPointerException" I think it's because my java program runs task very fast, it's almost at the same time. So computer do cmbBox.removeAllItems() and cmbBox.addItem(s) at the same time. Please help


K^2
  • K^2

    Vidi Vici Veni

  • Moderator
  • Joined: 14 Apr 2004
  • United-States
  • Best Poster in Technology / Programming 2017
    Best Poster [Technology / Programming] 2016
    Best Poster [Programming] 2015
    Most Knowledgeable [Web Development/Programming] 2013
    Most Knowledgeable [GTA Series] 2011
    Best Debater 2010

#2

Posted A week ago

No. Single thread guarantees execution in sequence.

Are you sure you have a row actually selected? Because if no row is selected, table.getSelectedRow() will return -1. So you'd be calling table.getValueAt(-1, 1), which returns null. This will also happen if you only have one column in your table, since you're asking for element from column 1, and counting starts with zero. In either case, calling equals on null will raise Null Pointer Exception which you see.

I see no other place where an exception can occur, but I'm rusty on Java. Your best bet is to step through this code with a debugger. If you don't know how to use one, you should look up some tutorials on Youtube. It will tell you exactly on which line the error happens and might provide you with a lot of other information. In general, being able to use debugger is a very useful skill.
  • Gian_Yagami likes this

Gian_Yagami
  • Gian_Yagami

    Mark Chump

  • Members
  • Joined: 08 Nov 2011
  • None

#3

Posted A week ago

No. Single thread guarantees execution in sequence.

Are you sure you have a row actually selected? Because if no row is selected, table.getSelectedRow() will return -1. So you'd be calling table.getValueAt(-1, 1), which returns null. This will also happen if you only have one column in your table, since you're asking for element from column 1, and counting starts with zero. In either case, calling equals on null will raise Null Pointer Exception which you see.

I see no other place where an exception can occur, but I'm rusty on Java. Your best bet is to step through this code with a debugger. If you don't know how to use one, you should look up some tutorials on Youtube. It will tell you exactly on which line the error happens and might provide you with a lot of other information. In general, being able to use debugger is a very useful skill.


I am really sure I got the row index since I put that code on event mouse clicked. Also I have 9 column in my table. Okay I will follow your advice. I have no idea with debug before, I had used once but still don't get it. Anyway thanks

K^2
  • K^2

    Vidi Vici Veni

  • Moderator
  • Joined: 14 Apr 2004
  • United-States
  • Best Poster in Technology / Programming 2017
    Best Poster [Technology / Programming] 2016
    Best Poster [Programming] 2015
    Most Knowledgeable [Web Development/Programming] 2013
    Most Knowledgeable [GTA Series] 2011
    Best Debater 2010

#4

Posted A week ago

Oh, I see one more potential issue. Your while loop is while(i<=cmbBox.getItemCount()). Again, since counting starts with 0, when i==cmbBox.getItemCount(), you're out of bounds. Try replacing that with a strict i<cmbBox.getItemCount() and see if you still get that exception. If that's not it, I'm definitely out of ideas.

Gian_Yagami
  • Gian_Yagami

    Mark Chump

  • Members
  • Joined: 08 Nov 2011
  • None

#5

Posted A week ago

Finally I got solution!
I leave cmbBox itemstatechanged to fill textbox function (boolean). So when cmbBox has empty, program still have boolean value true and fill texbox will run. It makes inconsistency between cmbBox and textbox. Sorry I am not posting full code because it messed up. I am fixing with change boolean value to false and after task completed I changed again to true.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users