Null Pointer - Tried to resolve... cant :-(

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lost Prophet
    New Member
    • Mar 2008
    • 14

    Null Pointer - Tried to resolve... cant :-(

    I posted this NullPointerExce ption error a week ago. Ive made progress as now parts of it are being populated. 'report' all throughout the other non-listed classes is being populated. 'gui' all throughout the other non-listed classes is being populated too. 'report' in DCWorkbenchGUI is not being populated and 'gui' in ReportData is not being populated. Both of these are the problems and I have tried everything to get them to work. Any help would be much appreciated... thanks

    btw, I have not included all the code as there is far too much of it

    ReportData class in report package
    [CODE=java]
    import dcw.script.*;
    import dcw.gui.*;
    import dcw.controller. *;

    public class ReportData
    {
    public DCWorkbenchGUI gui;
    public ComplexScript script;

    //flags for the future predictions
    public boolean flagPolicyChang ed = false;

    //state variables for the script
    public int publicDisorder = 0;
    public int soldierInjuries = 0;
    public int publicInjuries = 0;
    public int civilanFireInju ries = 0;
    public int civilanFireFata lities = 0;
    public int soldierFataliti es = 0;
    public int suspectApprehen sion = 0;
    public int baseDamage = 0;
    public int pressCoverage = 0;

    /**
    * Constructor for the ReportData thread
    *
    * @param g the gui
    * @param c the controller
    * @param s the script
    */
    public ReportData(DCWo rkbenchGUI g, ComplexScript s)
    {
    gui = g;
    script = s;
    }

    public void reportInfo()
    {
    System.out.prin tln("ReportInfo GUI: " + gui);

    String Report = ("Public Disorder (0 - 100): " + publicDisorder +"\n" +
    "Soldier Injuries (max 27): " + soldierInjuries +"\n" +
    "Public Injuries (max 20): " + publicInjuries +"\n" +
    "Civilian Emergency Services Injuries (max 10): " + civilanFireInju ries +"\n" +
    "Soldier Fatalities (max 15): " + soldierFataliti es +"\n" +
    "Civilian Emergency Services Fatalities (max 10): " + civilanFireFata lities +"\n" +
    "Suspect Capture likelihood (0 - 100): " + suspectApprehen sion +"\n" +
    "Base Damage (0 - 100): " + baseDamage +"\n" +
    "Press Coverage During Incident (0 - 100): " + pressCoverage);

    //display info dialog to the user
    gui.scenInfo("T he following information has been found by your assistant \n" + Report,
    "General Information",
    script.currentT imeInSecs());

    }

    public void assistantAdvice ()
    {

    System.out.prin tln("flagPolicy Changed: " + flagPolicyChang ed);

    if(flagPolicyCh anged)
    {
    String Probabilities = ("Changing the Access Policy to: \n" +
    " a) 'Nobody' may limit the sharing of information \n" +
    " b) 'Authorised' or 'Essential Only' may jeopardise confidential information \n" +
    "Changing the Exit Policy to: \n" +
    " a) 'Nobody' may increase the possibility of suspect apprehension by 20% \n" +
    " b) 'Essential Only' may increase the possibility of suspect apprehension by 15% \n" +
    " c) 'Authorised' may not affect the possibility of suspect apprehension");

    //display info dialog to the user
    gui.scenInfo("Y our assistant has predicted the following: \n" + Probabilities,
    "General Information",
    script.currentT imeInSecs());

    flagPolicyChang ed = false;
    }
    }[/CODE]

    DCWorkbenchGUI Class in gui package
    [CODE=java]
    import dcw.gui.*;
    import dcw.report.*;
    import dcw.controller. *;

    public class DCWorkbenchGUI extends JFrame
    {
    private ComplexScript script;
    private ReportData report;

    /**
    * Constructor for DCWorkbenchGUI
    *
    * @param c the central controlling script
    */
    public DCWorkbenchGUI( ComplexScript c, ReportData r)
    {
    initComponents( report);
    setVisible(true );
    script = c;
    report = r;

    System.out.prin tln("Report: " + report);
    }

    /**
    * This method is called from within the constructor to initialize the GUIs components.
    */
    private void initComponents( ReportData report)
    {
    System.out.prin tln("InitComp: " + report);

    Report.addMouse Listener(new java.awt.event. MouseAdapter()
    {
    public void mouseClicked(ja va.awt.event.Mo useEvent evt)
    {
    reportMouseClic ked(evt);
    }
    });

    AssistantAdvice .addMouseListen er(new java.awt.event. MouseAdapter()
    {
    public void mouseClicked(ja va.awt.event.Mo useEvent evt)
    {
    assistantAdvice MouseClicked(ev t);
    }
    });

    /**
    * Action for the pressing of the button, upon pressing the button, the button
    * is removed from the GUI, and the script resumes action.
    *
    * @param evt MouseEvent for the pressing of the button
    */

    private void reportMouseClic ked(java.awt.ev ent.MouseEvent evt)
    {
    report.reportIn fo();
    }

    private void assistantAdvice MouseClicked(ja va.awt.event.Mo useEvent evt)
    {
    System.out.prin tln("MouseClick ed: " + report);
    report.assistan tAdvice();
    }
    }
    [/CODE]

    ComplexScript Class in script package
    [CODE=java]
    import dcw.gui.*;
    import dcw.report.*;
    import dcw.controller. *;

    public class ComplexScript
    {
    //variables for control of the workbench
    public DCWorkbenchGUI gui;
    public VppController control = new VppController() ;
    public ReportData report;
    private short cid;
    private PrintWriter pw;

    /**
    * Main Method for the workbench.
    *
    * @param args the command line arguments
    */
    public static void main(String args[])
    {
    ComplexScript test = new ComplexScript() ;
    test.run();
    }

    /**
    * The run method for ComplexScript
    */
    public void run()
    {
    try
    {
    //initialise the gui, passing the script and report data
    gui = new DCWorkbenchGUI( this, report);

    report = new ReportData(gui, this);
    }
    catch(Exception e)
    {

    }
    }[/CODE]
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Which line was reported for the nullpointer?

    Comment

    • Lost Prophet
      New Member
      • Mar 2008
      • 14

      #3
      Various lines, but it depends what part of execution it is at. I have a button (Assistant Advice) which it errors on...

      DCWorkbenchGUI Class in gui package - report is null
      Code:
      .
      .
      .
      report.assistantAdvice();
      .
      .
      .
      ReportData class in report package - gui is null
      Code:
           
      public void reportInfo()
      {
      .
      .
      .
      .
      
               gui.scenInfo("The following information has been found by your assistant \n" + Report,
                           "General Information", 
                           script.currentTimeInSecs());  
      
      }
      .
      .
      .


      But, putting System.out.prin tln(report) within the DCWorkbenchGUI class is coming out null and also putting System.out.prin tln(gui) within the ReportData class is also coming out null.

      In both classes System.out.prin tln(script) is populated with the correct value so I dont see how report and gui isnt either as it is all set up in the same way

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        1.) You are passing a null report on line 33. At that time the variable report has not yet been initialized and is still null.
        2.) Do not use empty catch blocks. They hide exceptions instead of handling them.

        Comment

        • Lost Prophet
          New Member
          • Mar 2008
          • 14

          #5
          I saw that while trying to work around it a few days ago. The problem is changing this generates what appears to be a worse situation...

          Code:
                      //initialise the gui, passing the script and report data
                      gui = new DCWorkbenchGUI(this, report);
                      
                      report = new ReportData(gui, this);
          putting them that way round means that 'report' is null in DCWorkbenchGUI but putting them the other way round means that 'gui' is null in ReportData. I need both 'report' and 'gui' to be populated in ComplexScript so im struggling to see a way around this. My experience of Java isnt the greatest although I am getting by with it... any advice will be much appreciated

          Thanks

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            So, to create the gui you need the report and to create the report you need the gui?
            Chicken and egg if you ask me.
            Surely one of them doesn't require the other?

            Comment

            • Lost Prophet
              New Member
              • Mar 2008
              • 14

              #7
              it does seem strange but this is the set-up...

              The ReportData class calls upon a part of the DCWorkbenchGUI class in the gui package to create a dialog box

              The DCWorkbenchGUI class calls upon a part of the ReportData class in the report package using actionlisteners to know when the user requests the information that the ReportData generates

              The aim of the project was to seperate two environments which were interlinked in the script package. Ive done this by creating the report package but been hit with this problem. Can it be resolved or based on the above is it impossible?

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by Lost Prophet
                it does seem strange but this is the set-up...

                The ReportData class calls upon a part of the DCWorkbenchGUI class in the gui package to create a dialog box

                The DCWorkbenchGUI class calls upon a part of the ReportData class in the report package using actionlisteners to know when the user requests the information that the ReportData generates

                The aim of the project was to seperate two environments which were interlinked in the script package. Ive done this by creating the report package but been hit with this problem. Can it be resolved or based on the above is it impossible?
                Shouldn't you be creating the report inside the DCWorkbenchGUI class then(compositio n)?

                Comment

                • Nepomuk
                  Recognized Expert Specialist
                  • Aug 2007
                  • 3111

                  #9
                  Originally posted by Lost Prophet
                  The aim of the project was to seperate two environments which were interlinked in the script package. Ive done this by creating the report package but been hit with this problem. Can it be resolved or based on the above is it impossible?
                  I haven't been through everything above, but if both require each other, you'd have to create one of them with a dummy of the other and then switch it. Something like this:
                  [CODE=Java]
                  public class A
                  {
                  // The dummy A-class with the standard constructor
                  }

                  public class B
                  {
                  private A myA;
                  public B(A a)
                  {
                  //...
                  myA = A;
                  //...
                  }
                  public void switch(A a)
                  {
                  myA = A;
                  }
                  }

                  public class A2 extends A
                  {
                  // The actual A-class
                  public A2(B b)
                  {
                  //...
                  }
                  }
                  [/CODE]Hope that helps.

                  Greetings,
                  Nepomuk

                  Comment

                  • Lost Prophet
                    New Member
                    • Mar 2008
                    • 14

                    #10
                    Many other classes within the script package refer to the report package so 'report' needs to be populated shortly after the gui is generated at the latest.

                    I'll take a look into using the dummy methods

                    I now have just the one NullPointerExce ption in...

                    Code:
                           
                    if(flagPolicyChanged)
                    {
                                String Probabilities = ("Changing the Access Policy to: \n" +
                                                        "  a) 'Nobody' may limit the sharing of information \n" +
                                                        "  b) 'Authorised' or 'Essential Only' may jeopardise confidential information \n" +
                                                        "Changing the Exit Policy to: \n" +
                                                        "  a) 'Nobody' may increase the possibility of suspect apprehension by 20% \n" +
                                                        "  b) 'Essential Only' may increase the possibility of suspect apprehension by 15% \n" +
                                                        "  c) 'Authorised' may not affect the possibility of suspect apprehension");    
                    
                                      //display info dialog to the user
                                      [B]gui.scenInfo[/B]("Your assistant has predicted the following: \n" + Probabilities,
                                         "General Information", 
                                         script.currentTimeInSecs());  
                                 
                                 flagPolicyChanged = false;
                    }
                    Its progress but still no result

                    Comment

                    • Lost Prophet
                      New Member
                      • Mar 2008
                      • 14

                      #11
                      DONE... and it was soooo simple... For future reference if anyone has a similar I problem I did this...

                      DCWorkbenchGUI class in gui package - Added 'this' as a parameter into the Assistant Advice method in the ReportData class in the report package
                      Code:
                          
                      private void assistantAdviceMouseClicked(java.awt.event.MouseEvent evt) 
                          {         
                              report.assistantAdvice(this);
                          }
                      ReportData class in report package - Added DCWorkbenchGUI gui into the Assistant Advice method to take in the gui parameter that had been sent from DCWorkbenchGUI
                      Code:
                          
                      public void assistantAdvice(DCWorkbenchGUI gui)
                          {
                              
                             System.out.println("flagPolicyChanged: " + flagPolicyChanged);
                             System.out.println("AssistantAdvice GUI: " + gui);
                             
                             if(flagPolicyChanged)
                             {
                                  String Probabilities = ("Changing the Access Policy to: \n" +
                                                          "  a) 'Nobody' may limit the sharing of information \n" +
                                                          "  b) 'Authorised' or 'Essential Only' may jeopardise confidential information \n" +
                                                          "Changing the Exit Policy to: \n" +
                                                          "  a) 'Nobody' may increase the possibility of suspect apprehension by 20% \n" +
                                                          "  b) 'Essential Only' may increase the possibility of suspect apprehension by 15% \n" +
                                                          "  c) 'Authorised' may not affect the possibility of suspect apprehension");    
                      
                                        //display info dialog to the user
                                        gui.scenInfo("Your assistant has predicted the following: \n" + Probabilities,
                                           "General Information", 
                                           script.currentTimeInSecs());  
                                   
                                   flagPolicyChanged = false;
                              }
                      Now both gui and report are populated in the exact same areas and objective 2 appears to be complete :-D

                      Thanks for your help people

                      Comment

                      • Nepomuk
                        Recognized Expert Specialist
                        • Aug 2007
                        • 3111

                        #12
                        Glad you solved it. :-)

                        Greetings,
                        Nepomuk

                        Comment

                        Working...