Results 1 to 6 of 6
  1. #1
    talha06's Avatar
    talha06 is offline Member
    Join Date
    Jul 2009
    Location
    Turkey
    Posts
    4
    Rep Power
    0

    Exclamation [SOLVED] Null pointer exception

    Hello to everyone,

    I take an annoying exception while taking values from database..

    Here's my code, it gives the exception at line 22(I put a sign)..

    Thanx in advance..

    Java Code:
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    
    import com.mysql.jdbc.Statement;
    
    public class deneme {
    	public static void main(String args[]) {
    		try{
    			String[] adlar = null;
    			int indis= 0;
    			Class.forName("com.mysql.jdbc.Driver").newInstance();
    			Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ogrdb","root", "");
    			Statement st = (Statement) con.createStatement();
    			String sorgu = "select AdSoyad from ogr";
    			ResultSet rs = st.executeQuery(sorgu);
    			while(rs.next()) {
    				if(rs.getString("AdSoyad") != null) {
    		====>	  adlar[indis] = rs.getString("AdSoyad");
    				  System.out.println("DB'den cekilen: " + adlar[indis]);
    				  indis++;
    				}
    			}
    			rs.close();
    			st.close();
    			con.close();
    		} catch(Exception e) {
    			e.printStackTrace();
    		}
    }
    }
    Last edited by talha06; 07-14-2009 at 01:12 AM. Reason: solved

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Do you see anything wrong with this?
    Java Code:
    String[] adlar = null;
    // .... code deleted
    adlar[indis] = rs.getString("AdSoyad"); // null pointer exception here
    Myself, I'd use perhaps an ArrayList here.

  3. #3
    talha06's Avatar
    talha06 is offline Member
    Join Date
    Jul 2009
    Location
    Turkey
    Posts
    4
    Rep Power
    0

    Default

    can someone help me to fix this exception? :confused:

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by talha06 View Post
    can someone help me to fix this exception? :confused:
    ???

    I thought that I just did. Again, you are setting a String array to null and then trying to use this array. You shouldn't be surprised that this throws a null pointer exception.

    solution: don't try to use a null array. Use an arraylist and be sure to initialize it before trying to use it.

    If you need to read up on ArrayLists, then please check out the Sun tutorials.
    Last edited by Fubarable; 07-14-2009 at 01:00 AM.

  5. #5
    talha06's Avatar
    talha06 is offline Member
    Join Date
    Jul 2009
    Location
    Turkey
    Posts
    4
    Rep Power
    0

    Default

    OK I got it.. Thanx a lot dear 'Fubarable'..

    With regards..
    Talha

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  2. Null pointer exception
    By Stephenmak in forum New To Java
    Replies: 5
    Last Post: 04-01-2009, 02:17 PM
  3. Null Pointer Exception
    By andre1011 in forum Advanced Java
    Replies: 4
    Last Post: 02-07-2009, 03:30 AM
  4. Null Pointer Exception
    By Jacinth in forum New To Java
    Replies: 4
    Last Post: 01-22-2009, 01:47 PM
  5. Null Pointer Exception
    By demiser55 in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 06:33 PM

Tags for this Thread

Posting Permissions

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