Results 1 to 6 of 6
Thread: Null pointer exception
- 07-13-2009, 11:30 PM #1
[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
-
Do you see anything wrong with this?
Myself, I'd use perhaps an ArrayList here.Java Code:String[] adlar = null; // .... code deleted adlar[indis] = rs.getString("AdSoyad"); // null pointer exception here
- 07-14-2009, 12:35 AM #3
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.
- 07-14-2009, 01:10 AM #5
OK I got it.. Thanx a lot dear 'Fubarable'..
With regards..
Talha
-
Similar Threads
-
Null pointer Exception
By peiceonly in forum New To JavaReplies: 8Last Post: 09-05-2010, 06:48 PM -
Null pointer exception
By Stephenmak in forum New To JavaReplies: 5Last Post: 04-01-2009, 02:17 PM -
Null Pointer Exception
By andre1011 in forum Advanced JavaReplies: 4Last Post: 02-07-2009, 03:30 AM -
Null Pointer Exception
By Jacinth in forum New To JavaReplies: 4Last Post: 01-22-2009, 01:47 PM -
Null Pointer Exception
By demiser55 in forum New To JavaReplies: 1Last Post: 09-22-2008, 06:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks