[英]how can i correct my error in iterating over an arraylist of products?
this is the method that list the products by keyword:::"produitsParMC" after selecting these products from database,note that i have created a class for product,my problem in that when i try to fetch these products by keyword an exception saying "Exception in thread "main" java.lang.NullPointerException at dao.App.main(App.java:21)"这是从数据库中选择这些产品后按关键字:::“produitsParMC”列出产品的方法,请注意,我为产品创建了一个类,我的问题是当我尝试通过关键字获取这些产品时出现异常说“ dao.App.main(App.java:21) 处的线程“main”java.lang.NullPointerException 中的异常”
public class ProduitDaoImpl implements IProduitDao {
public Produit save(Produit p) {
Connection connection=SingletonConnection.getConnection();
try {
PreparedStatement ps=connection.prepareStatement("INSERT INTO PRODUITS(DESIGNATION,PRIX,QUANTITE) VALUES (?,?,?)");
ps.setString(1, p.getDesignation());
ps.setDouble(2, p.getPrix());
ps.setInt(3, p.getQuantite());
ps.executeUpdate();
PreparedStatement ps2=connection.prepareStatement("SELECT MAX(ID) as MAXID FROM PRODUITS");
ResultSet rs=ps2.executeQuery();
if(rs.next()){
//if because we have one value to view,,next: in order to positionate in the first enregistrement
p.setId(rs.getLong("MAXID"));
}
ps.close();
//connection.close();//si on utilise un singletoon on ne doit pas fermer la connectio car elle est crée une seule fois
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return null;
}
public List<Produit> produitsParMC(String mc) {
List<Produit> produits=new ArrayList<Produit>();
Connection connection=SingletonConnection.getConnection();
try {
PreparedStatement ps=connection.prepareStatement("SELECT * FROM PRODUITS WHERE DESIGNATION LIKE ?");
ps.setString(1, mc);
ResultSet rs=ps.executeQuery();
while(rs.next()){
Produit p=new Produit();
p.setId(rs.getLong("ID"));
/*System.out.println(p.getId()+"cccc");*/
p.setDesignation(rs.getString("DESIGNATION"));
p.setPrix(rs.getDouble("Prix"));
p.setQuantite(rs.getInt("QUANTITE"));
produits.add(p);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public Produit getProduit(Long id) {
// TODO Auto-generated method stub
return null;
}
public Produit update(Produit p) {
// TODO Auto-generated method stub
return null;
}
public void deleteProduit(Long id) {
// TODO Auto-generated method stub
}
}
here is my main class这是我的主要课程
package dao;
import java.util.ArrayList;
import java.util.List;
public class App {
public static void main(String[] args) {
ProduitDaoImpl dao=new ProduitDaoImpl();
Produit p4=new Produit("mac 000", 222, 111);
dao.save(p4);
System.out.println(p4.toString());
List<Produit> pro=new ArrayList<Produit>();
pro= dao.produitsParMC("asus 6500");
System.out.println("my list is :");
for(Produit p :pro){
System.out.println(p.toString());
}
}
}
finally here is the output containing the error最后这里是包含错误的输出
Produit [id=195, designation=mac 000, prix=222.0, quantite=111]
my list is :
Exception in thread "main" java.lang.NullPointerException
at dao.App.main(App.java:21)
The output of produitsParMC is null since in your function you return null;
produitsParMC 的输出为空,因为在您的函数中您return null;
you should return the ArrayList你应该返回 ArrayList
At this pro= dao.produitsParMC("asus 6500");
在此pro= dao.produitsParMC("asus 6500");
line, pro
is null
, so your for each loop in this case looks like this: line, pro
为null
,因此在这种情况下您的 for 每个循环如下所示:
for(Produit p : null) {
System.out.println(p.toString());
}
That produces a NullPointerException
.这会产生一个NullPointerException
。 Instead of returning null
at the end of your ProduitDaoImpl#produitsParMC
method, return the produits
ArrayList which you create at the beginning of that method.不要在ProduitDaoImpl#produitsParMC
方法结束时返回null
,而是返回您在该方法开始时创建的produits
ArrayList。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.