Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7686123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:24:29+00:00 2026-05-31T19:24:29+00:00

So I am supposed to make 3 classes and am given a 4th class

  • 0

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).

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….

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<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.

}

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.

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<DBrecord> myDBTable;


    public DBTable()
    {
        ArrayList<DBrecord> myDBTable= new ArrayList<DBrecord>();

    }


    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<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<DBBinding> myDBrecord;


    public DBrecord()
    {
        //DBrecord myRecord=new DBrecord();
        select=false;
        ArrayList<DBBinding> myDbrecord=new ArrayList<DBBinding>();
    }

    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<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<Array.getLength(temp); i++)    ///for testing
            //{System.out.println(temp[i]);}
        for(int i=0; i<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;}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:24:30+00:00Added an answer on May 31, 2026 at 7:24 pm

    In your constructor: ArrayList<DBBinding> myDbrecord=new ArrayList<DBBinding>();

    You only create a local variable named myDbrecord and initialize it, instead of initializing the field myDBrecord.

    You probably wanted instead:

    myDBrecord = new ArrayList<DBBinding>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes under QT, one to make a form, the other to
Supposed I have the following string: string str = <tag>text</tag>; And I would like
I have a base class Element and derived two other classes from it, Solid
I'm used to writing classes like this: public class foo { private string mBar
I have the following classes / interfaces: public interface IProjectRepository { IQueryably<Project> GetProjects(); }
I have a function that 2 derived classes use, but the third doesn't, would
It seems to me that introducing an ORM tool is supposed to make your
Suppose the jQuery object of the input is $input. How to make its value
Suppose I have an element called #container-main . How do I make that display:none
One of my Eclipse plug-ins (OSGi bundles) is supposed to contain a directory (

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.