Dreaded Null Pointer Exceptions (Java)

greenspun.com : LUSENET : Steve Heller's books : One Thread

I am working through Who's Afraid of Java. In doing one of the exercises, I wrote a DrugStockItem class, a derived class, PrescriptionDrugItem, and an application program. All three compiled successfully. But when I ran the application, I got a NullPointerException message. The code that is causing the problem is certainly this:

void DeductSaleFromInventory(int QuantitySold) { String EnterPassword = ""; System.out.println("Password?"); EnterPassword = RWVar.readString(System.in); if (m_Password.equals(EnterPassword)) m_InStock -= QuantitySold; else System.out.println("Access Denied. Wrong Password."); }

Why is this code fine with the compiler, but causes Java to choke at runtime? Any suggestions?

I couldn't get the compiler on the CD to work (perhaps because I had already downloaded the compiler from the Sun web site). So the compiler I am using is jdk1_1_8.

-- Cliff Wirt (DementedMagpie@hotmail.com), May 03, 1999


Moderation questions? read the FAQ