java - Strange NullPointer Exception in main -
i'm trying exercise java book. code comes , have not added code besides setting path database. i'm on osx, had install apache derby. everytime build , run program this:
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 , jsp $49.50 mcb2 murach's mainframe cobol $59.50 sqls murach's sql sql server $49.50 zjcl murach's os/390 , z/os jcl $62.50 exception in thread "main" java.lang.nullpointerexception first product: @ dbtesterapp.printproduct(dbtesterapp.java:117) @ dbtesterapp.printfirstproduct(dbtesterapp.java:66) @ dbtesterapp.main(dbtesterapp.java:16) java result: 1 build successful (total time: 2 seconds)
i'm confused why exception keeps happening. don't seem find wrong 'main' code (see below) , feel i've tried everything. clue causing this?
import java.sql.*; public class dbtesterapp { private static connection connection = null; public static void main(string args[]) { // connection , start derby engine connection = murachdb.getconnection(); if (connection != null) system.out.println("derby has been started.\n"); // select data database printproducts(); printfirstproduct(); printlastproduct(); printproductbycode("java"); // modify data in database product p = new product("test", "test product", 49.50); insertproduct(p); printproducts(); deleteproduct(p); printproducts(); // disconnect 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 * 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(); // debugging } } public static void printfirstproduct() { product p = null; // add code prints record first product in products table system.out.println("first product:"); printproduct(p); system.out.println(); } public static void printlastproduct() { product p = null; // add code prints record last product in products table system.out.println("last product:"); printproduct(p); system.out.println(); } public static void printproductbycode(string productcode) { product p = null; // add code prints product specified code system.out.println("product code: " + productcode); printproduct(p); system.out.println(); } public static void insertproduct(product p) { system.out.println("insert test: "); // add code inserts specified product database // if product specifed code exists, display error message printproduct(p); system.out.println(); } private static void deleteproduct(product p) { system.out.println("delete test: "); // add code deletes specified product database // if product specified code doesn't exist, display error message printproduct(p); system.out.println(); } // use method print product object on 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); } }
this sequence of code produce npe
product
p
has not been instantiated:
product p = null; system.out.println("first product:"); printproduct(p);
Comments
Post a Comment