Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

java.lang.NullPointerException

Thread view: 
tolu45 - 16 Nov 2005 09:34 GMT
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[i].setNodeID(i);
          Nodes[i].setFFlag(false);
          Nodes[i].setPrimaryKey("");
          Nodes[i].setSFlag(false);
          Nodes[i].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[i].getNodeID()==0)
           {
               this.Nodes[i].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[i].getFrontColName()) {
               Edges[j].setFrontNode(i);
               Edges[i].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[i].getFrontColName()) {
               Edges[j].setFrontNode(i);
               Edges[i].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;
   }
 
}
Andrew Thompson - 16 Nov 2005 09:55 GMT
> 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
Roedy Green - 16 Nov 2005 12:51 GMT
>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

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Robert Waters - 16 Nov 2005 19:42 GMT
<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[i] = n;

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

>            Nodes[i].setNodeID(i);
>            Nodes[i].setFFlag(false);
[quoted text clipped - 13 lines]
>         // 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){
[quoted text clipped - 6 lines]
>         Nodes[idx].addCondition(cdt);
>     }

<snipped rest of code>

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

Bob


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.