I have a java program which I want to use it to create table in MS SQL server database. The connection seems successful. But I got NullPointerException. I need help about it. My program and the error is followed. Thank you so much for your help.
C:\Project>java CreateTables Connecting to Billpayment... Connection successful
Driver Information Driver Name: SQLServer Driver Version: 2.2.0040
Database Information Database Name: Microsoft SQL Server Database Version: Microsoft SQL Server 2000 - 8.00.194 (Intel X86) Aug 6 2000 00:57:48 Copyright (c) 1988-2000 Microsoft Corporation Enterprise Evaluation Edition on Windows NT 5.1 (Build 2600: Service Pack 1)
Creating tables... System Exception in createAccountTable java.lang.NullPointerException java.lang.NullPointerExceptio n at CreateTables.createAccountTable(CreateTables.java:94) at CreateTables.create(CreateTables.java:35) at CreateTables.main(CreateTables.java:24)
Try tu use connection = DriverManager.getConnection( ... instead.
Explanation:
class C {
Connection c;
public C () { method1(); method2(); }
void method1 { Connection c = createConnection(); //At this point you should assing c to this.c } void method2 { c.doSomethingVeryImportant(); //this will result in a NullPointerException, because you never assinged c from method1 to this.c } }