Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2014
    Posts
    2
    Rep Power
    0

    Default Problem With Class References

    Hello,
    I'm having a problem with classes that reference each other (I think). I keep getting a Java Null Pointer Exception when working on the following Applet. I have several classes that rely on each other. Initially I had all of the class constructors setting private variables to point to the objects they relied on. I kept getting NullPointerExceptions, so I set the constructors to blank, then used setters so that each class could talk to each other. This works like a beauty when I program in C++, but I think Java works differently and I'm not sure how to achieve the same effect. Any help would be much appreciated, thank you!

    Java Code:
    package mainPkg;
    import javax.swing.*;
    
    import java.awt.*;
    import java.awt.event.*;
    
    enum ReportType{
    	BOOKING, BILLING, BACKLOG
    }
    
    
    
    public class CMain extends JApplet{
    	private CGUI GUI;
    	private CDBWrapper sx;
    	private CReport report;
    	private CListener events;
    	
    public void init(){
    	GUI = new CGUI();
    	report = new CReport();
    	events = new CListener();
    	
    	try{
    		sx = new CDBWrapper();
    	}
    	catch(Throwable exc){
    		System.out.println("Unable to initialize database, error: " + exc);
    	}
    	
    	
    	report.initialize(GUI,sx);
    	sx.setReport(report);
    	report.setType(ReportType.BOOKING);
    	events.setListener(report);
    	report.initialize(GUI, sx);
    	
    	GUI.init(this, events);
    	
    	setSize(800,600);
    	
    	
    	
    }
    	
    
    
    
    
    
    
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Problem With Class References

    Please include any error messages, otherwise we are just guessing. And it is probably a safe bet that
    if it worked in C++ and not in Java then your C++ code had a hidden error somewhere that never manifested itself.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Join Date
    Aug 2014
    Posts
    2
    Rep Power
    0

    Default Re: Problem With Class References

    Here is the error I'm getting:

    Unable to initialize database, error: java.lang.NullPointerException
    java.lang.NullPointerException
    at mainPkg.CMain.init(CMain.java:33)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

    Let me know if there is anything else I can do to clarify. I'm pretty new to Java!

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Problem With Class References

    I suggest you print out the values of the references you are creating to see what is null.
    The sx looks like it could be null. But since I don't have the supporting classes, there
    is not much I can do to help.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. About references.
    By fatabass in forum New To Java
    Replies: 8
    Last Post: 03-03-2012, 09:17 AM
  2. class extending combining with references
    By Isty in forum New To Java
    Replies: 2
    Last Post: 11-22-2011, 12:40 AM
  3. Replies: 20
    Last Post: 06-03-2009, 06:33 PM
  4. Replies: 8
    Last Post: 03-21-2009, 10:34 AM

Posting Permissions

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