Java main中奇怪的空指针异常
我想做一个java书上的练习。代码按原样提供,除了设置数据库路径之外,我没有向代码中添加任何内容。我在OSX上,所以我必须安装ApacheDerby。每次我构建和运行程序时,我都会得到以下信息:Java main中奇怪的空指针异常,java,nullpointerexception,osx-mountain-lion,derby,Java,Nullpointerexception,Osx Mountain Lion,Derby,我想做一个java书上的练习。代码按原样提供,除了设置数据库路径之外,我没有向代码中添加任何内容。我在OSX上,所以我必须安装ApacheDerby。每次我构建和运行程序时,我都会得到以下信息: Derby has been started. Product list: bvbn Murach's Beginning Visual Basic .NET $49.50 cshp Murach's C# $4
Derby has been started.
Product list:
bvbn Murach's Beginning Visual Basic .NET $49.50
cshp Murach's C# $49.50
java Murach's Beginning Java $49.50
jsps Murach's Java Servlets and JSP $49.50
mcb2 Murach's Mainframe COBOL $59.50
sqls Murach's SQL for SQL Server $49.50
zjcl Murach's OS/390 and z/OS JCL $62.50
Exception in thread "main" java.lang.NullPointerException
First product:
at DBTesterApp.printProduct(DBTesterApp.java:117)
at DBTesterApp.printFirstProduct(DBTesterApp.java:66)
at DBTesterApp.main(DBTesterApp.java:16)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
我不明白为什么这个例外会持续发生。我似乎没有发现“主”代码有任何错误(见下文),我觉得我已经尝试了所有方法。有什么线索可以解释是什么原因造成的吗
import java.sql.*;
public class DBTesterApp
{
private static Connection connection = null;
public static void main(String args[])
{
// get the connection and start the Derby engine
connection = MurachDB.getConnection();
if (connection != null)
System.out.println("Derby has been started.\n");
// select data from database
printProducts();
printFirstProduct();
printLastProduct();
printProductByCode("java");
// modify data in the database
Product p = new Product("test", "Test Product", 49.50);
insertProduct(p);
printProducts();
deleteProduct(p);
printProducts();
// disconnect from the database
if (MurachDB.disconnect())
System.out.println("Derby has been shut down.\n");
}
public static void printProducts()
{
try (Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM Products"))
{
Product p = null;
System.out.println("Product list:");
while(rs.next())
{
String code = rs.getString("ProductCode");
String description = rs.getString("Description");
double price = rs.getDouble("Price");
p = new Product(code, description, price);
printProduct(p);
}
System.out.println();
}
catch(SQLException e)
{
e.printStackTrace(); // for debugging
}
}
public static void printFirstProduct()
{
Product p = null;
// add code that prints the record for the first product in the Products table
System.out.println("First product:");
printProduct(p);
System.out.println();
}
public static void printLastProduct()
{
Product p = null;
// add code that prints the record for the last product in the Products table
System.out.println("Last product:");
printProduct(p);
System.out.println();
}
public static void printProductByCode(String productCode)
{
Product p = null;
// add code that prints the product with the specified code
System.out.println("Product by code: " + productCode);
printProduct(p);
System.out.println();
}
public static void insertProduct(Product p)
{
System.out.println("Insert test: ");
// add code that inserts the specified product into the database
// if a product with the specifed code already exists, display an error message
printProduct(p);
System.out.println();
}
private static void deleteProduct(Product p)
{
System.out.println("Delete test: ");
// add code that deletes the specified product from the database
// if a product with the specified code doesn't exist, display an error message
printProduct(p);
System.out.println();
}
// use this method to print a Product object on a single line
private static void printProduct(Product p)
{
String productString =
StringUtils.padWithSpaces(p.getCode(), 8) +
StringUtils.padWithSpaces(p.getDescription(), 44) +
p.getFormattedPrice();
System.out.println(productString);
}
}
由于未实例化
产品p
,此代码序列将生成NPE
:
Product p = null;
System.out.println("First product:");
printProduct(p);
由于未实例化产品p
,此代码序列将生成NPE
:
Product p = null;
System.out.println("First product:");
printProduct(p);
私有静态无效打印产品(产品p)
{
字符串产品字符串=
StringUtils.padWithSpaces(p.getCode(),8)+/private static void打印产品(产品p)
{
字符串产品字符串=
StringUtils.padWithSpaces(p.getCode(),8)+/在这里传递null
public static void printFirstProduct()
{
Product p = null;
// add code that prints the record for the first product in the Products table
System.out.println("First product:");
printProduct(p);
System.out.println();
}
你在这里传递空值
public static void printFirstProduct()
{
Product p = null;
// add code that prints the record for the first product in the Products table
System.out.println("First product:");
printProduct(p);
System.out.println();
}
另一个可能的弱点:
connection = MurachDB.getConnection();
if (connection != null)
System.out.println("Derby has been started.\n");
如果connection
为null
,则代码仍会继续执行,然后尝试调用null
上的方法
我并不是说它现在会给你带来问题,但在某个时候肯定会有问题。另一个可能的弱点:
connection = MurachDB.getConnection();
if (connection != null)
System.out.println("Derby has been started.\n");
如果connection
为null
,则代码仍会继续执行,然后尝试调用null
上的方法
我并不是说它现在会导致您的问题,但在某个时候肯定会造成问题。您正在声明p=null
,然后将其传递给一个试图访问其成员之一的方法。除了null ref之外,您为什么会期望任何结果?第117行是“printProduct(Product p)”中的“String productString=”'方法p
成为printProducts()中的Product
对象
方法。我在想,因为它是第一个运行的方法,并且p
成为产品
对象,p
不再为null。但它仍然会给出一个错误。您声明p=null
,然后将其传递给试图访问其成员之一的方法。您为什么希望得到null以外的任何结果-ref?第117行是“printProduct(Product P)”方法中的“String productString=”P
成为printProducts()中的Product
对象
method。我在想,因为它是第一个运行的方法,并且p
成为Product
对象,p
不再是空的。但是它仍然给出了一个错误。确定后,我不得不在DBTesterApp类中声明私有静态产品p=null;
,并删除剩余的Product p=null;
>在所有拥有它的方法中。程序现在编译得很好。好的,结果证明我必须在DBTesterApp类中声明私有静态产品p=null;
,并删除所有拥有它的方法中剩余的产品p=null;
。程序现在编译得很好。