[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

NullPointerException ???

Posted on 2004-04-18
4
Medium Priority
?
277 Views
Last Modified: 2010-03-31
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.NullPointerException
        at accountServicesInterface.getAllEntries(accountServicesInterface.java:24)
        at accountServicesInterface.main(accountServicesInterface.java:79)

=-=-= java code -=-=-
package com.bedlam.accountServices.common;

import com.bedlam.accountServices.database.*;
import com.bedlam.accountServices.common.LogWriter;

import java.sql.*;

public class accountServicesInterface {
  private Database db;
  private Table table;
  private String tableName;

  public accountServicesInterface(String 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("UserClient"))
      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("UserClient")) {
      criteria = "USERID="+id;
      field = "USERID";
    } else {
      criteria = "MERCID="+id;
      field = "MERCID";
    }

    boolean isValid = isEntryValid(criteria,field);
    while (!isValid) {
      id = generateRandomID();
      isValid = isEntryValid(criteria,field);
    }
    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("accountServicesInterface",
                      "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("accountServicesInterface",
                      "getEntry",
                      "Exception while executing SQL statement",
                      e);
    }
  }
}
0
Comment
Question by:blizz_ard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 92

Accepted Solution

by:
objects earned 140 total points
ID: 10856974
> Table table = db.getTable(tableName);

change that to:

table = db.getTable(tableName);
0
 
LVL 92

Expert Comment

by:objects
ID: 10856977
your existing code initialises a local var named table, instead of the table member var.
0
 
LVL 1

Expert Comment

by:KartikShah
ID: 10859693
The problem seems to be in your constructor. You current code is :

  public accountServicesInterface(String tableName) {
    db = new Database();
    Table table = db.getTable(tableName);
    this.tableName = tableName;
  }


while it should be:

  public accountServicesInterface(String 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
0
 
LVL 92

Expert Comment

by:objects
ID: 10864427
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question