2013-05-14 93 views
1

我想从一本java书做练习。代码是这样的,除了设置数据库的路径之外,我没有向代码添加任何内容。我在OSX上,所以我必须安装Apache Derby。每次我建立和运行该程序,我得到这个:奇怪的空指针异常主

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); 
} 
} 
+1

什么是117线? – GriffeyDog 2013-05-14 18:09:45

+2

您正在声明'p = null',然后将它传递给试图访问其成员之一的方法。你为什么期望除了null-ref以外的任何结果? – dlev 2013-05-14 18:11:36

+0

第117行是:'printProduct(Product P)'方法中的'String productString =' – oxxi 2013-05-14 18:15:12

回答

8

的代码序列将产生NPEProductp尚未实例:

Product p = null; 

System.out.println("First product:"); 
printProduct(p); 
+0

事实证明,我必须在DBTesterApp类中声明'private static Product p = null;',并在所有拥有它的方法中移除剩余的'Product p = null;'。该程序现在完美编译。 – oxxi 2013-05-14 19:08:03

1
private static void printProduct(Product p) 
{ 
    String productString = 
     StringUtils.padWithSpaces(p.getCode(), 8) + // <-- This guy is angry when the 'p' passed to it is null 
     StringUtils.padWithSpaces(p.getDescription(), 44) + 
     p.getFormattedPrice(); 

    System.out.println(productString); 
} 
} 

然后

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); //<--- This 'p' is null when you pass it to it because you never assgin it after setting it to null. 
    System.out.println(); 
} 
1

你传入空在这里

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(); 
} 
0

另一个可能弱点:

connection = MurachDB.getConnection(); 
if (connection != null) 
    System.out.println("Derby has been started.\n"); 

如果connectionnull代码向前走反正后来试图调用上null方法。

我不是说现在会导致你的问题,但肯定可以在某些时候。