i have the folowing code in my jsp page:

String[] name = null;
String[] manufacturer = null;
String[] description = null;
String[] category = null;
String[] price = null;
String result = null;
String connectionURL = "jdbc:mysql://localhost:3306/syte"; 
Connection connection = null; 								Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL,"root","");		
Statement statement = null;
statement = connection.createStatement();
PreparedStatement pstatement = null;
pstatement = connection.prepareStatement("SELECT name, manufacturer, description, category, price FROM product;");
ResultSet updateQuery; 
updateQuery = pstatement.executeQuery();
int i = 0;
updateQuery.beforeFirst();
while(updateQuery!=null && updateQuery.next())
{
name[i] = updateQuery.getString(1);
manufacturer[i] = updateQuery.getString(2);
description[i] = updateQuery.getString(3);
category[i] = updateQuery.getString(4);
price[i] = updateQuery.getString(5);
i++;
updateQuery.next();
}

i get the folowing error on line 19:
Null poiter exception

the database has the folowing structure:

name manufacturer description category product_id price
test test test test 1 999
test1 test1 test1 test1 2 999

Recommended Answers

All 2 Replies

Why do you call the updateQuery.next() inside the loop?
You call it at the end of the loop and at the beginning. So you are missing a row each time you loop.
Also you get that exception there because the array name is null.

Delete what you have. Don't use that many arrays. Create a single object with attributes the name, manufacturer, description,... and add get/set methods.
Then have an array of that object.

class Product {
  String name=null;
  ...
  double price=0;

  // constructor
  // get/set methods
}

But in this case you shouldn't use arrays because you don't know the size. You don't know how many elements you will get.
Use ArrayLists:

ArrayList list = new ArrayList();
list.add( /*the instance of your object*/ );

Product prod = (Product)list.get(i);

Put that code in a method that returns that ArrayList and close the ResultSet, PreparedStatement and Connection.

changed code to this :

final class prod
{
private String name ;
private String manufacturer ;
private String description;
private String category;
private String price;
public void setName(String s) 
{
name = s; 
} 
public String getName() 
{
return name; 
}
public void setDescription(String s) 
{
description = s; 
}
public String getDescription() 
{
return description; 
}
public void setManufacturer(String s) 
{
manufacturer = s; 								    }
}
public String getManufacturer() 
{
return manufacturer; 
}
public void setCategory(String s) 
{
category = s; 
} 
public String getCategory() 
{
return category; 
}
public void setPrice(String s) 
{
price = s; 
} 
public String getPrice() 
{
return price; 
}
								    
}
final prod[] p = null;	   
String connectionURL = "jdbc:mysql://localhost:3306/syte"; 
Connection connection = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root","");	
Statement statement = null;
statement = connection.createStatement();
PreparedStatement pstatement = null;
pstatement = connection.prepareStatement("SELECT name, manufacturer, description, category, price FROM product;");
ResultSet updateQuery;
updateQuery = pstatement.executeQuery();
updateQuery.beforeFirst();								
int i = 0;
while(updateQuery!=null && updateQuery.next())
{
p[i].setName(updateQuery.getString(1));
p[i].setManufacturer(updateQuery.getString(2));
p[i].setDescription(updateQuery.getString(3));
p[i].setCategory(updateQuery.getString(4));
p[i].setPrice(updateQuery.getString(5));

i++;
}

i get same error at line 65

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.