CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2008
    Posts
    2

    Little help thanks in advance! :D

    Ok, my problem is I have an issue with a button on my program.

    I have a JTable I need to read, the problem is, everytime I try to, a Null Pointer Exception jumps out, and then reads the first line of the JTable. The problem, I don't want that exception to jump everytime the JTable has a null space.

    this is my code on the for loop to read the JTable:

    Code:
        for(int z=0;z<tablaGenera.getRowCount();++z)
        {
            for(int y=0;y<tablaGenera.getColumnCount();++y)
            {
                String tabla= tablaGenera.getValueAt(z,y).toString();
                System.out.print(tabla+",");
            }
        }
    Please help me, I perhaps am missing something I didn't learned well. If I have only 1 line on the JTable, I get the NullPointer Exception, but if my Table is all filled with data (no null spaces) then I get no Exception. And what I need is it to read without any error even if there's only 1 line.

    Thanks in advance :3

  2. #2
    Join Date
    Feb 2008
    Posts
    966

    Re: Little help thanks in advance! :D

    The problem is that you are trying to call the .toString() method on objects that are null. You can prevent this, or catch this.

    Prevent:
    if(table.getValueAt(x,y) != null) {
    table.getValueAt(x,y).toString();
    }


    Catch it (not recommended)
    try {
    table.getValueAt(x,y).toString();
    } catch(NullPointerException e) {
    //silently ignore the expected error
    }

  3. #3
    Join Date
    Dec 2008
    Posts
    2

    Re: Little help thanks in advance! :D

    Ok, thx very much.. it helped me a lot! :3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured