blizz_ard
asked on
NullPointerException ???
Hi.. I keep getting NullPointerException and can't seem to figure out what is wrong with my code? Anybody have any ideas? Thanks in advance
=-=-= stdout -=-=-=-
Exception in thread "main" java.lang.NullPointerExcep tion
at accountServicesInterface.g etAllEntri es(account ServicesIn terface.ja va:24)
at accountServicesInterface.m ain(accoun tServicesI nterface.j ava:79)
=-=-= java code -=-=-
package com.bedlam.accountServices .common;
import com.bedlam.accountServices .database. *;
import com.bedlam.accountServices .common.Lo gWriter;
import java.sql.*;
public class accountServicesInterface {
private Database db;
private Table table;
private String tableName;
public accountServicesInterface(S tring tableName) {
db = new Database();
Table table = db.getTable(tableName);
this.tableName = tableName;
}
public Row getEntry(String crit) throws SQLException {
return table.getRow(crit);
}
public RowSet getAllEntries(String crit) throws SQLException {
return table.getRows(crit);
}
public int generateRandomID() {
if (tableName.equals("UserCli ent"))
return (int)(Math.random()*15000) ;
else
return (int)(Math.random()*30000+ 15000);
}
public int generateValidatedRandomID( ) throws SQLException {
int id = generateRandomID();
String criteria,field;
if (tableName.equals("UserCli ent")) {
criteria = "USERID="+id;
field = "USERID";
} else {
criteria = "MERCID="+id;
field = "MERCID";
}
boolean isValid = isEntryValid(criteria,fiel d);
while (!isValid) {
id = generateRandomID();
isValid = isEntryValid(criteria,fiel d);
}
return id;
}
public boolean isEntryValid(String crit, String field) throws SQLException {
Row row = getEntry(crit);
String isValid = row.get(field);
if (isValid.equals(null))
return false;
else
return true;
}
public void insertEntry(Row entryRow) {
try {
table.putRow(entryRow);
} catch(SQLException e) {
LogWriter.write("accountSe rvicesInte rface",
"insertEntry",
"Exception while inserting new row",
e);
}
}
public static void main(String args []) {
accountServicesInterface ac
= new accountServicesInterface(" userClient ");
try {
RowSet rows = ac.getAllEntries("userid>0 ");
} catch(SQLException e) {
LogWriter.write("accountSe rvicesInte rface",
"getEntry",
"Exception while executing SQL statement",
e);
}
}
}
=-=-= stdout -=-=-=-
Exception in thread "main" java.lang.NullPointerExcep
at accountServicesInterface.g
at accountServicesInterface.m
=-=-= java code -=-=-
package com.bedlam.accountServices
import com.bedlam.accountServices
import com.bedlam.accountServices
import java.sql.*;
public class accountServicesInterface {
private Database db;
private Table table;
private String tableName;
public accountServicesInterface(S
db = new Database();
Table table = db.getTable(tableName);
this.tableName = tableName;
}
public Row getEntry(String crit) throws SQLException {
return table.getRow(crit);
}
public RowSet getAllEntries(String crit) throws SQLException {
return table.getRows(crit);
}
public int generateRandomID() {
if (tableName.equals("UserCli
return (int)(Math.random()*15000)
else
return (int)(Math.random()*30000+
}
public int generateValidatedRandomID(
int id = generateRandomID();
String criteria,field;
if (tableName.equals("UserCli
criteria = "USERID="+id;
field = "USERID";
} else {
criteria = "MERCID="+id;
field = "MERCID";
}
boolean isValid = isEntryValid(criteria,fiel
while (!isValid) {
id = generateRandomID();
isValid = isEntryValid(criteria,fiel
}
return id;
}
public boolean isEntryValid(String crit, String field) throws SQLException {
Row row = getEntry(crit);
String isValid = row.get(field);
if (isValid.equals(null))
return false;
else
return true;
}
public void insertEntry(Row entryRow) {
try {
table.putRow(entryRow);
} catch(SQLException e) {
LogWriter.write("accountSe
"insertEntry",
"Exception while inserting new row",
e);
}
}
public static void main(String args []) {
accountServicesInterface ac
= new accountServicesInterface("
try {
RowSet rows = ac.getAllEntries("userid>0
} catch(SQLException e) {
LogWriter.write("accountSe
"getEntry",
"Exception while executing SQL statement",
e);
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
your existing code initialises a local var named table, instead of the table member var.
The problem seems to be in your constructor. You current code is :
public accountServicesInterface(S tring tableName) {
db = new Database();
Table table = db.getTable(tableName);
this.tableName = tableName;
}
while it should be:
public accountServicesInterface(S tring tableName) {
db = new Database();
this.table = db.getTable(tableName);
this.tableName = tableName;
}
The problem here was, that you are initializing a local variable table, in your constructor, thile, you should have referenced to the variable table, which is a member variable.
This seems to be the reason for getting the NullPointerException
public accountServicesInterface(S
db = new Database();
Table table = db.getTable(tableName);
this.tableName = tableName;
}
while it should be:
public accountServicesInterface(S
db = new Database();
this.table = db.getTable(tableName);
this.tableName = tableName;
}
The problem here was, that you are initializing a local variable table, in your constructor, thile, you should have referenced to the variable table, which is a member variable.
This seems to be the reason for getting the NullPointerException