Results 1 to 4 of 4
  1. #1
    Jeremy720 is offline Member
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Default Instantiation order

    Hello. I'm trying to populate a combo box using an MVC model, and am using either serialization, or a database based on the users preference. I'm not sure exactly what's going on, but it's a visibility issue between the listener, controller, and interface classes upon instantiation...

    FROM THE LISTENER:
    Java Code:
    public Listener(Controller pc)
    {
      this.pc = pc;
      foodChoices = pc.getFoodChoices
         (categoryList.getSelectedItem().toString());
      foodList = new JComboBox(foodChoices);
      ...
      con.add(foodList);
      ...
    }
    
    FROM THE CONTROLLER:
    
    public class Controller()
    {
      public static void main(String[] args) 
      { 			
        Controller app = newController();
        app.initialize();
      }
    
    public void initialize() 		
    {		
      pl = new Listener(this);
     dir = System.getProperty("user.dir");
     appPath = dir.substring(0,dir.lastIndexOf('\\'));
     while (check)
     {
        String daoAccess = JOptionPane.showInputDialog(null, 
        "Enter S to read/write to text file, D to read/write to database");
     	
      if (daoAccess != null)
     {
          if (daoAccess.equalsIgnoreCase("d"))
         {
            sd = new SQLDAO(this);
            pl = new Listener(this);
            check = false;
          }
          else if (daoAccess.equalsIgnoreCase("s"))
          {
             dir = System.getProperty("user.dir");
            appPath = dir.substring(0,dir.lastIndexOf('\\'));
            pl = new ProteinListener(this);
            sd = new SerialDAO(appPath); 
            check = false;
           }
           else if (daoAccess == "")
           {
              System.out.println("You must enter a data access type");
              check = true;
           }
           else
          {
            Utility3.errorMessage("Invalid entry");
             check = true;
           }
       } 
       else
        break;
      }    	
     }
    (METHOD FOR READING DATABASE/FILE FOR COMBO BOX POPULATION):
    public String[] getFoodChoices(String category)
    {
       return dao.getFoodChoices(category);
    }
    
    }
    RUNTIME ERROR:
    NullPointerException
    @ Controller.getFoodChoices
    @ Listener.<init>
    @ Controller.initialize
    @ Controller.main

    Any ideas?

    Thanks so much. this is a personal project I'm doing over the summer.
    Last edited by Fubarable; 07-14-2009 at 07:33 PM. Reason: code tags added

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by Jeremy720 View Post
    Hello. I'm trying to populate a combo box using an MVC model, and am using either serialization, or a database based on the users preference. I'm not sure exactly what's going on, but it's a visibility issue between the listener, controller, and interface classes upon instantiation...

    FROM THE LISTENER:
    public Listener(Controller pc)
    {
    this.pc = pc;
    foodChoices = pc.getFoodChoices
    (categoryList.getSelectedItem().toString());
    foodList = new JComboBox(foodChoices);
    ...
    con.add(foodList);
    ...
    }

    FROM THE CONTROLLER:

    public class Controller()
    {
    public static void main(String[] args)
    {
    Controller app = newController();
    app.initialize();
    }

    public void initialize()
    {
    pl = new Listener(this);
    dir = System.getProperty("user.dir");
    appPath = dir.substring(0,dir.lastIndexOf('\\'));
    while (check)
    {
    String daoAccess = JOptionPane.showInputDialog(null,
    "Enter S to read/write to text file, D to read/write to database");

    if (daoAccess != null)
    {
    if (daoAccess.equalsIgnoreCase("d"))
    {
    sd = new SQLDAO(this);
    pl = new Listener(this);
    check = false;
    }
    else if (daoAccess.equalsIgnoreCase("s"))
    {
    dir = System.getProperty("user.dir");
    appPath = dir.substring(0,dir.lastIndexOf('\\'));
    pl = new ProteinListener(this);
    sd = new SerialDAO(appPath);
    check = false;
    }
    else if (daoAccess == "")
    {
    System.out.println("You must enter a data access type");
    check = true;
    }
    else
    {
    Utility3.errorMessage("Invalid entry");
    check = true;
    }
    }
    else
    break;
    }
    }
    (METHOD FOR READING DATABASE/FILE FOR COMBO BOX POPULATION):
    public String[] getFoodChoices(String category)
    {
    return dao.getFoodChoices(category);
    }

    }

    RUNTIME ERROR:
    NullPointerException
    @ Controller.getFoodChoices
    @ Listener.<init>
    @ Controller.initialize
    @ Controller.main

    Any ideas?

    Thanks so much. this is a personal project I'm doing over the summer.
    Can you post your code in CODE tags?
    It makes it really difficult to see what's going on otherwise.

    However, what is "dao" in food choices, and where is it initialised?
    I'm guessing a bit, but I think this is the culprit:

    Java Code:
    pl = new Listener(this);
    This calls the getFoodChoices, however that happens before the bulk of the Controller has been initialised...so you get your NPE.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I've edited the original post to add code tags. To the original poster, please use tags when posting code so that your code retains its formatting and is thus readable. To do this, highlight the posted code and press the code button in the forum editor. Good luck!

  4. #4
    Jeremy720 is offline Member
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Default got it

    thank you. firstly...I will post with 'code' tags'
    secondly...thanks for your help. I had to wait to instantiate the listener class until the remainder of the controller class had run.

Similar Threads

  1. Instantiation using an instance factory method
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-29-2008, 01:35 PM
  2. instantiation
    By Camden in forum New To Java
    Replies: 1
    Last Post: 03-02-2008, 05:44 PM
  3. Instantiation
    By kavithas in forum New To Java
    Replies: 2
    Last Post: 12-05-2007, 05:56 PM
  4. Instantiation using an instance factory method
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 09-26-2007, 09:25 PM
  5. object instantiation and arrayList
    By lockmac in forum New To Java
    Replies: 5
    Last Post: 08-09-2007, 07:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •