Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Causing NULL POINTER when i add a code;

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Location
    Philippines
    Posts
    19
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post Causing NULL POINTER when i add a code;

    public DefaultTableModel showTable(DefaultTableModel tmodel, String sql){
            DefaultTableModel tm = tmodel;
            String sq = sql;
     
     
           try {
               st=con.createStatement();
               rs=st.executeQuery(sq);
               System.out.println("Query: "+sq);
     
               ResultSetMetaData rmeta = (ResultSetMetaData)rs.getMetaData();
               int numColumns = rmeta.getColumnCount();
               System.out.println("Column Count: "+numColumns);
     
               for(int i=1;i<=numColumns;i++){
                   if(i<=numColumns){
                       System.out.println(rmeta.getColumnName(i));
                       tm.addColumn(rmeta.getColumnName(i));
                   } 
               }
     
     
     
           } catch (SQLException ex) {
               Logger.getLogger(JDBCCrud.class.getName()).log(Level.SEVERE, null, ex);
           }
     
     
     
        return tm;
        }//SHOWTABLE



    when i add: tm.addColumn(rmeta.getColumnName(i));
    the program will show errors which says that it was a NULL POINTER and lead me to this code.

    im getting the name of column to the database. and i used this code: System.out.println(rmeta.getColumnName(i)); to check if java gets the data from sqlserver database.


  2. #2
    Member coderxx0's Avatar
    Join Date
    Feb 2013
    Location
    England, UK
    Posts
    61
    My Mood
    Cool
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Causing NULL POINTER when i add a code;

    Quote Originally Posted by ZDreamer08 View Post
    public DefaultTableModel showTable(DefaultTableModel tmodel, String sql){
            DefaultTableModel tm = tmodel;
            String sq = sql;
     
     
           try {
               st=con.createStatement();
               rs=st.executeQuery(sq);
               System.out.println("Query: "+sq);
     
               ResultSetMetaData rmeta = (ResultSetMetaData)rs.getMetaData();
               int numColumns = rmeta.getColumnCount();
               System.out.println("Column Count: "+numColumns);
     
               for(int i=1;i<=numColumns;i++){
                   if(i<=numColumns){
                       System.out.println(rmeta.getColumnName(i));
                       tm.addColumn(rmeta.getColumnName(i));
                   } 
               }
     
     
     
           } catch (SQLException ex) {
               Logger.getLogger(JDBCCrud.class.getName()).log(Level.SEVERE, null, ex);
           }
     
     
     
        return tm;
        }//SHOWTABLE



    when i add: tm.addColumn(rmeta.getColumnName(i));
    the program will show errors which says that it was a NULL POINTER and lead me to this code.

    im getting the name of column to the database. and i used this code: System.out.println(rmeta.getColumnName(i)); to check if java gets the data from sqlserver database.
    is this the same code as this?

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Location
    Philippines
    Posts
    19
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Causing NULL POINTER when i add a code;

    no. im disecting my code. so i found the error and it was in the tm.addColumn(rmeta.getColumnName(i));

  4. #4
    Member coderxx0's Avatar
    Join Date
    Feb 2013
    Location
    England, UK
    Posts
    61
    My Mood
    Cool
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Causing NULL POINTER when i add a code;

    Quote Originally Posted by ZDreamer08 View Post
    no. im disecting my code. so i found the error and it was in the tm.addColumn(rmeta.getColumnName(i));
    ok have you fixed it? or still need assistance?

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Location
    Philippines
    Posts
    19
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Causing NULL POINTER when i add a code;

    still need assistance and as i debug it. i have a bad feeling that it's because of the DefaultTableModel tm; is being the NULL POINTER error. cause when im getting the row information from database, it has the error NULL POINTER too that goes to tm.addRow(data);

  6. #6
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Causing NULL POINTER when i add a code;

    Are you sure the table model isn't null when being passed in?

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Causing NULL POINTER when i add a code;

    For faster and likely more useful replies, include the full text of the error message(s) with the code and your question

Similar Threads

  1. Null Pointer Excpetion help......
    By nutan in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 3rd, 2013, 10:24 PM
  2. Need Help with Null Pointer Exception
    By kendraheartt in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 23rd, 2012, 02:20 PM
  3. Annoying null pointer exception in my code
    By oyinig in forum Member Introductions
    Replies: 1
    Last Post: July 13th, 2012, 08:06 PM
  4. [SOLVED] Null Pointer Exception
    By wltrallen2 in forum Object Oriented Programming
    Replies: 7
    Last Post: May 27th, 2012, 10:21 AM
  5. [SOLVED] What's causing this Null Pointer Exception?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 29
    Last Post: July 2nd, 2011, 10:34 PM

Tags for this Thread