Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing multiple classes getting Null pointer exception. Probably syntactical.
    text
    copied!<p>So I am supposed to make 3 classes and am given a 4th class to use for a user interface. One class (DBBinding) is supposed to have a String key and String value and take something like name:Alien or star: harry dean and make name or star be the "key" and the other is the "value" the next class (DBrecord) is to hold a group of these "bindings" as one record. I have chosen to keep a group of these bindings in a ArrayList. The third class(DBTable) is another ArrayList but of . I am at the point where I am reading in a line of txt from file where each line of txt is going to be one DBrecord that we know will be in correct formatting(key:value, key:value, key:value, and so on). </p> <p>Where I am having trouble is within the DBrecord class. I have a method(private void addBindingToRecord(String key_, String value_)) that is called from (public static DBrecord createDBrecord(String record)) from within the DBrecord class here are each methods code. I am having trouble with the addBindingToRecord method ... it null pointer exceptions on the first time used. I think it has to do with sytax and how I am calling the "this.myDBrecord.add(myDBBinding);"... have tried it multiple ways with same result....</p> <pre><code>public static DBrecord createDBrecord(String record)//takes a string and breaks it into DBBindings and makes a record with it. { DBrecord myRecord=new DBrecord(); String temp[]; temp=record.split(",",0); if(temp!=null) { for(int i=0; i&lt;Array.getLength(temp); i++) { System.out.println("HERE");//for testing String temp2[]; temp2=temp[i].split(":",0); myRecord.addBindingToRecord(temp2[0], temp2[1]); } } return myRecord; } private void addBindingToRecord(String key_, String value_) { DBBinding myDBBinding=new DBBinding(key_, value_); if(myDBBinding!=null)//////////////ADDED this.myDBrecord.add(myDBBinding);///Here is where my null pointer exception is. } </code></pre> <p>I am going to post the full code of all my classes here so you have it if need to look at. Thank for any help, hints, ideas. </p> <pre><code>package DataBase; import java.io.*; public class CommandLineInterface { public static void main(String[] args) { DBTable db = new DBTable(); // DBTable to use for everything try { // Create reader for typed input on console BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String line; while (true) { int length = 0; int selectedLength = 0; // YOUR CODE HERE System.out.println("\n" + length + " records (" + selectedLength + " selected)"); System.out.println("r read, p print, sa select and, so select or, da ds du delete, c clear sel"); System.out.print("db:"); line = reader.readLine().toLowerCase(); if (line.equals("r")) { System.out.println("read"); String fname; System.out.print("Filename:"); //fname = reader.readLine();////ADD BACK IN AFTER READ DEBUGED // YOUR CODE HERE fname="movie.txt"; db.readFromFile(fname); } else if (line.equals("p")) { System.out.println("print"); // YOUR CODE HERE DBTable.print(); } else if (line.equals("da")) { System.out.println("delete all"); // YOUR CODE HERE } else if (line.equals("ds")) { System.out.println("delete selected"); // YOUR CODE HERE } else if (line.equals("du")) { System.out.println("delete unselected"); // YOUR CODE HERE } else if (line.equals("c")) { System.out.println("clear selection"); /// YOUR CODE HERE } else if (line.equals("so") || line.equals("sa")) { if (line.equals("so")) System.out.println("select or"); else System.out.println("select and"); System.out.print("Criteria record:"); String text = reader.readLine(); // get text line from user // YOUR CODE HERE } else if (line.equals("q") || line.equals("quit")) { System.out.println("quit"); break; } else { System.out.println("sorry, don't know that command"); } } } catch (IOException e) { System.err.println(e); } } } package DataBase; import java.util.*; import java.io.*; public class DBTable { static ArrayList&lt;DBrecord&gt; myDBTable; public DBTable() { ArrayList&lt;DBrecord&gt; myDBTable= new ArrayList&lt;DBrecord&gt;(); } public static void addRecordToTable(DBrecord myRecord)//added static when added addRecordToTable in readFromFile { if(myRecord!=null) {myDBTable.add(myRecord);} } public static void readFromFile(String FileName) { try { FileReader myFileReader=new FileReader(FileName); String line="Start"; BufferedReader myBufferdReader=new BufferedReader(myFileReader); while(line!="\0") { line=myBufferdReader.readLine(); if(line!="\0") { System.out.println(line);//TEST CODE addRecordToTable(DBrecord.createDBrecord(line));// made addRecordToTable static. } } }catch(IOException e) {System.out.println("File Not Found");} } public static void print() { if (myDBTable==null) { System.out.println("EMPTY TABLE"); return; } else { for (int i=0; i&lt;myDBTable.size(); i++) { System.out.println(myDBTable.get(i).toString()); } } } } package DataBase; import java.util.*; import java.lang.reflect.Array; //import DataBase.*;//did not help ... ? public class DBrecord { boolean select; String key; //need some type of collection to keep bindings. ArrayList&lt;DBBinding&gt; myDBrecord; public DBrecord() { //DBrecord myRecord=new DBrecord(); select=false; ArrayList&lt;DBBinding&gt; myDbrecord=new ArrayList&lt;DBBinding&gt;(); } private void addBindingToRecord(String key_, String value_) { DBBinding myDBBinding=new DBBinding(key_, value_); //System.out.println(myDBBinding.toString());//for testing if(myDBBinding!=null)//////////////ADDED this.myDBrecord.add(myDBBinding); System.out.println(key_);//for testing System.out.println(value_);//for testing } public String toString() { //out put key first then all values in collection/group/record. use correct formatting. StringBuilder myStringbuilder=new StringBuilder(); for (int i=0;i&lt;this.myDBrecord.size();i++) { myStringbuilder.append(myDBrecord.get(i).toString()); myStringbuilder.append(", "); } myStringbuilder.delete(myStringbuilder.length()-2, myStringbuilder.length()-1);//delete last ", " thats extra return myStringbuilder.toString(); } public static DBrecord createDBrecord(String record)//takes a string and breaks it into DBBindings and makes a record with it. { //System.out.println("HERE");//for testing DBrecord myRecord=new DBrecord(); String temp[]; temp=record.split(",",0); if(temp!=null) { //System.out.println("HERE");//for testing //for(int i=0; i&lt;Array.getLength(temp); i++) ///for testing //{System.out.println(temp[i]);} for(int i=0; i&lt;Array.getLength(temp); i++) { System.out.println("HERE");//for testing String temp2[]; temp2=temp[i].split(":",0); System.out.println(temp2[0]);//for testing System.out.println(temp2[1]);//for testing myRecord.addBindingToRecord(temp2[0], temp2[1]); System.out.println(temp2[0]+ " "+ temp2[1]);////test code } } return myRecord; } } package DataBase; public class DBBinding { private String key; private String value; public DBBinding(String key_, String value_) { key =key_; value=value_; } public String getKey() {return key;} public String getValue() {return value;} public String toString() {return key+": "+value;} } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload