java.lang.NullPointerException

Discussion in 'Java' started by tolu45, Nov 16, 2005.

  1. tolu45

    tolu45 Guest

    Hi all,
    I am having some problems with my implementation. what i want to do is
    this, i have some class called nodes, edges and selection graph. I
    instantiated a node in the selection graph in order to create a
    selection graph with only one node and no edges. When i to run the
    program so that the output will be this selection graph i got a null
    pointerexception error. Could anyone tell me how to solve this problem?
    I have include the code for the class below. of es make up a selection
    graph. here are the codes:

    Nodes class:
    public class Node {

    private double nodeID = 0;
    private String tableName = null;
    private String primaryKey = null;
    private Vector condition = new Vector();
    private boolean sFlag = false;
    private boolean fFlag = false;

    /** Creates a new instance of SelGrapNode
    public Node(String table_name, Vector condition, boolean node_flag,
    String primary_key) {
    this.tableName = table_name;
    this.condition = condition;
    this.sFlag = sFlag;
    this.fFlag = fFlag;

    }
    */

    public Node(){
    //nodeID = 0;
    }
    //modifier methods

    public double getNodeID () { return nodeID; }
    public void setNodeID(double node ) {
    nodeID = node;
    }

    public String getTableName () { return tableName; }
    public void setTableName(String tName ) {
    tableName = tName;
    }

    public Vector getCondition () { return condition; }

    public void addCondition(String cond){
    condition.addElement(cond);
    }

    public void remCondition(String indx){
    condition.remove(indx);
    }

    public boolean getSFlag () { return sFlag;}
    public void setSFlag(boolean sFlag){
    sFlag = sFlag;
    }

    public boolean getFFlag () { return fFlag;}
    public void setFFlag(boolean fFlag){
    fFlag= fFlag;
    }

    public String getPrimaryKey()
    {
    return primaryKey;
    }

    public void setPrimaryKey(String name)
    {
    primaryKey = name;
    }
    }

    Selection graph class:
    public class SelGraph {
    public Node[] Nodes;
    public Edge[] Edges;

    public SelGraph() {

    Nodes = new Node[10];
    for (int i =0; i<10; i++)
    {
    Nodes.setNodeID(i);
    Nodes.setFFlag(false);
    Nodes.setPrimaryKey("");
    Nodes.setSFlag(false);
    Nodes.setTableName("");
    }
    //Edges[] = new Edge[100];
    }

    //public jarray getNodes(){

    //}

    public void addNode(double node, String tblname, String prKey,
    boolean sFlg, boolean fFlg, String cdt){
    // always add new node to the end of array
    int lastIndex = Nodes.length;

    Nodes[lastIndex].setNodeID(node);
    Nodes[lastIndex].setTableName(tblname);
    Nodes[lastIndex].setPrimaryKey(prKey);
    Nodes[lastIndex].setSFlag(sFlg);
    Nodes[lastIndex].setFFlag(fFlg);
    Nodes[lastIndex].addCondition(cdt);
    }

    public void addNode(int idx, double node, String tblname, String
    prKey, boolean sFlg, boolean fFlg, String cdt){

    Nodes[idx].setNodeID(node);
    Nodes[idx].setTableName(tblname);
    Nodes[idx].setPrimaryKey(prKey);
    Nodes[idx].setSFlag(sFlg);
    Nodes[idx].setFFlag(fFlg);
    Nodes[idx].addCondition(cdt);
    }

    public void addEdge(double fNode, double bNode, String fColNm,
    String bColNm, boolean eFlg) {
    int lastIndex = Edges.length;

    Edges[lastIndex].setFrontNode(fNode);
    Edges[lastIndex].setBackNode(bNode);
    Edges[lastIndex].setFrontColName(fColNm);
    Edges[lastIndex].setBackColName(bColNm);
    Edges[lastIndex].setEFlag(eFlg);
    }

    public void addEdge(int idx, double fNode, double bNode, String
    fColNm, String bColNm, boolean eFlg) {

    Edges[idx].setFrontNode(fNode);
    Edges[idx].setBackNode(bNode);
    Edges[idx].setFrontColName(fColNm);
    Edges[idx].setBackColName(bColNm);
    Edges[idx].setEFlag(eFlg);
    }

    public void addNodeAndEdge(double node, String tblname, String
    prKey, boolean sFlg, boolean fFlg, String cdt, double fNode, double
    bNode, String fColNm, String bColNm, boolean eFlg){
    this.addNode(node, tblname, prKey, sFlg, fFlg, cdt);
    this.addEdge(fNode, bNode, fColNm, bColNm, eFlg);
    }

    public void addPositiveCondition(int idx, String cdt){
    this.Nodes[idx].addCondition(cdt);
    }

    // FIXME: This Are the remaining Methods for the refinement: We need
    to know where to include them
    public void addNegativeCondition(int idx,String cdt){

    int size = Nodes.length;

    for (int i =0; i < size; i++) {
    if (Nodes.getNodeID()==0)
    {
    this.Nodes.addCondition(cdt);
    }
    if (Nodes[idx].getNodeID()!=0){
    this.Edges[idx].setBackNode(idx-1);
    this.Nodes[idx].addCondition(cdt);
    this.Nodes[idx].setSFlag(false);
    }
    }

    }

    public void addPresentEdgeAndOpenNode(int idx, String cdt){
    int size = Edges.length;
    for (int i = 0; i< size; i++){
    for (int j = 0 ; j < i; j++){
    if(Edges[j].getBackColName()== Edges.getFrontColName()) {
    Edges[j].setFrontNode(i);
    Edges.setEFlag(true);
    }
    }
    }

    }

    public void addAbsentEdgeAndclosesNode(int idx){
    int size = Edges.length;
    for (int i = 0; i< size; i++){
    for (int j = 0 ; j < i; j++){
    if(Edges[j].getBackColName()== Edges.getFrontColName()) {
    Edges[j].setFrontNode(i);
    Edges.setEFlag(false);
    }
    }
    }
    }

    public Vector refinement(){
    Vector entropyVec = new Vector();
    return entropyVec;
    }
    }

    edge class:
    package DMengine;

    /**
    *
    * @author Adesola + Jorge + Tu Ba
    */
    public class Edge {
    private double frontNode = 0;
    private double backNode = 0;
    private String frontColName = new String();
    private String backColName = new String();
    private boolean eFlag = false;

    /**
    * Creates a new instance of Edge

    public Edge(double frontNode, double backNode, String frontColName,
    String backColName, boolean eFlag ) {
    this.frontNode = frontNode;
    this.backNode = backNode;
    this.frontColName = frontColName;
    this.backColName = backColName;
    this.eFlag = eFlag;
    }
    */
    public Edge(){
    }

    public double getFronNode() {
    return this.frontNode;
    }

    public void setFrontNode(double fNode){
    this.frontNode = fNode;
    }

    public double getBackNode() {
    return this.backNode;
    }

    public void setBackNode(double bNode){
    this.backNode = bNode;
    }

    public String getFrontColName() {
    return this.frontColName;
    }

    public void setFrontColName(String fColname){
    this.frontColName = fColname;
    }

    public String getBackColName() {
    return this.backColName;
    }

    public void setBackColName(String bColname){
    this.backColName = bColname;
    }

    public boolean getEFlag () {
    return eFlag;
    }

    public void setEFlag(boolean eFl){
    eFlag = eFl;
    }

    }
     
    tolu45, Nov 16, 2005
    #1
    1. Advertising

  2. tolu45 wrote:

    > I am having some problems with my implementation. what i want to do is
    > this, i have some class called nodes, edges and selection graph. I
    > instantiated a node in the selection graph in order to create a
    > selection graph with only one node and no edges. When i to run the
    > program so that the output will be this selection graph i got a null
    > pointerexception error.


    'liar'. I predict you are getting a NullPointerException, and
    that sure is no 'Error'.

    Or to put that another way, please copy/paste the
    *exact* error or exception message you are getting.
    <http://www.physci.org/codes/javafaq.jsp#exact>

    >..Could anyone tell me how to solve this problem?
    > I have include the code for the class below. of es make up a selection
    > graph. here are the codes:


    Posting 266 lines of code that need to be split across
    three classes (in two packages) and have imports added
    before ..failing to display the problem, is hardly
    likely to result in productive help.

    Please consider posting only SSCCE's in theses instances.
    <http://www.physci.org/codes/sscce.jsp>

    The only advice I can provide based on the available
    information is very generic.
    <http://mindprod.com/jgloss/runerrormessages.html#NULLPOINTEREXCEPTION>

    And, as an aside, the best group for those new to
    Java is c.l.j.help
    <http://www.physci.org/codes/javafaq.jsp#cljh>

    HTH
     
    Andrew Thompson, Nov 16, 2005
    #2
    1. Advertising

  3. tolu45

    Roedy Green Guest

    On 16 Nov 2005 01:34:14 -0800, "tolu45" <> wrote,
    quoted or indirectly quoted someone who said :

    >I am having some problems with my implementation.


    Your headline said the problem was a NullPointerException but you left
    out the things needed to track such a beast:

    1. the stack track verbatim

    2. pointers in your source code to the lines it is complaining about.

    See
    http://mindprod.com/jgloss/runerrormessages.html#NULLPOINTEREXCEPTION

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 16, 2005
    #3
  4. <snipped some code>
    > public SelGraph() {
    >
    > Nodes = new Node[10];


    At this point you have made an array object, but each element of the array
    has nothing in it (null).

    > for (int i =0; i<10; i++)
    > {


    I think this starts your null pointer problems. Since Nodes[0] currently is
    null, then you cannot call a method on it.

    try making a node first

    Node n = new Node();
    Nodes = n;

    Now your array actually has a node in it, and you can call methods on it.

    > Nodes.setNodeID(i);
    > Nodes.setFFlag(false);
    > Nodes.setPrimaryKey("");
    > Nodes.setSFlag(false);
    > Nodes.setTableName("");
    > }
    > //Edges[] = new Edge[100];
    > }
    >
    > //public jarray getNodes(){
    >
    > //}
    >
    > public void addNode(double node, String tblname, String prKey,
    > boolean sFlg, boolean fFlg, String cdt){
    > // always add new node to the end of array
    > int lastIndex = Nodes.length;

    You have same problem here. Make the node object you are going to insert:
    Node n = new Node();
    Nodes[lastIndex] = n;

    Now the array actually has something. You also need to do some error
    checking - What if lastIndex > the size of your array. Now you will get an
    ArrayOutOfBounds Exception.

    >
    > Nodes[lastIndex].setNodeID(node);
    > Nodes[lastIndex].setTableName(tblname);
    > Nodes[lastIndex].setPrimaryKey(prKey);
    > Nodes[lastIndex].setSFlag(sFlg);
    > Nodes[lastIndex].setFFlag(fFlg);
    > Nodes[lastIndex].addCondition(cdt);


    I think you would need to increment lastIndex by one here so you do not
    overwrite the last insertion.

    > }


    I would consider using method chaining to avoid repeated logic errors from
    the evil cut and paste monster.

    >
    > public void addNode(int idx, double node, String tblname, String
    > prKey, boolean sFlg, boolean fFlg, String cdt){
    >
    > Nodes[idx].setNodeID(node);
    > Nodes[idx].setTableName(tblname);
    > Nodes[idx].setPrimaryKey(prKey);
    > Nodes[idx].setSFlag(sFlg);
    > Nodes[idx].setFFlag(fFlg);
    > Nodes[idx].addCondition(cdt);
    > }


    <snipped rest of code>

    I just glanced at this quickly, did not compile it, so consider it a hint.

    Bob
     
    Robert Waters, Nov 16, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dhek Bhun Kho
    Replies:
    0
    Views:
    2,238
    Dhek Bhun Kho
    Jul 9, 2003
  2. Victoria
    Replies:
    4
    Views:
    641
  3. Mr Smith
    Replies:
    7
    Views:
    13,600
    Mr Smith
    Feb 21, 2005
  4. Fahad
    Replies:
    1
    Views:
    591
    Andrew Thompson
    Aug 8, 2005
  5. Replies:
    7
    Views:
    7,551
    Anonymous
    Oct 14, 2005
Loading...

Share This Page