12 Replies - 1042 Views - Last Post: 07 February 2013 - 07:41 PM Rate Topic: -----

#1 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

Cannot make a static reference to the non-static method

Posted 30 January 2013 - 08:28 AM

ok, so I feel like a douche but have been stuck on something that is usually simple.

I have tested these separate and they work fine but when I went to marry them together I had the dreaded Cannot make a static reference to the non-static method

addListenerClass :
if(confirmAdd == JOptionPane.YES_OPTION)
         {
            Course coreTemp = new CoreCourse(
                  ToolBar.getCourseCodeInput().getText(),
                  ToolBar.getCourseTitleInput().getText(), 
                  ToolBar.getCreditPoints());
                  System.out.println(coreTemp.toString());
                  ProgramMap.addCorePanel(coreTemp);
         }


and ProgramMap :

public class ProgramMap extends JPanel {
   
   public ProgramMap(AMSView Frame){
      all the bits
   
   public void addElectivePanel(Course coreTemp){
   all the bits...
}


ProgramMap.addCorePanel(coreTemp); - is the problem...

It is something stupid but I can not remember how to fix it.

Thanks
floppy

This post has been edited by floppyspace: 30 January 2013 - 08:29 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Cannot make a static reference to the non-static method

#2 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: Cannot make a static reference to the non-static method

Posted 30 January 2013 - 08:38 AM

Just as you are not allowed to use the keyword "this" in a static content, it should remind you that static - can - refer - to static methods, AND Non-static - can - refer - to - both - static and Non-static.
Was This Post Helpful? 4
  • +
  • -

#3 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

Re: Cannot make a static reference to the non-static method

Posted 30 January 2013 - 08:51 AM

both classes are non static, that is what I am trying to avoid, but the call

ProgramMap.addCorePanel(coreTemp);

wants me to make my addCorePanel static.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7630
  • View blog
  • Posts: 12,866
  • Joined: 19-March 11

Re: Cannot make a static reference to the non-static method

Posted 30 January 2013 - 08:52 AM

Instance members (fields and methods) belong to particular objects, so a static method can't refer to them unless you tell them which instance you're pointing to. You need an object.

Typically, this comes up when you try to work straight out of main. This is bad practice. Main is just a hook that the jvm calls to start up a program: it should instantiate some objects and give them a push, and do no work of its own.
Was This Post Helpful? 1
  • +
  • -

#5 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

Re: Cannot make a static reference to the non-static method

Posted 30 January 2013 - 09:17 AM

The design is based on mvc, so I get my data from a Toolbar class (user input JPanel) - pass it to the listener then display it on the ProgramMap (or that is what I want), so it is not running directly from main if that helps in anyway.

Course coreTemp = new CoreCourse
- I thought this instantiates my object as a type of Course?

think sleep may help me :( , never enough time ....

Thanks
floppy
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7630
  • View blog
  • Posts: 12,866
  • Joined: 19-March 11

Re: Cannot make a static reference to the non-static method

Posted 30 January 2013 - 09:21 AM

ProgramMap.addCorePanel(coreTemp);


This is a static reference - you're calling this on the class ProgramMap, not on some particular ProgramMap object.

We're sort of shooting in the dark, though, since we can't see your actual source code.
Was This Post Helpful? 2
  • +
  • -

#7 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

Re: Cannot make a static reference to the non-static method

Posted 30 January 2013 - 09:41 AM

View Postjon.kiparsky, on 31 January 2013 - 12:21 AM, said:

ProgramMap.addCorePanel(coreTemp);


This is a static reference - you're calling this on the class ProgramMap, not on some particular ProgramMap object.


ahh, This makes sense although it will have to wait till some sleep.

So in my ProgramMap class I need an object of the Course to pass it to? or something similar

I think that is were my problem lays as it does not contain a Course object (which is what I am trying to add).

ProgramMap

public class ProgramMap extends JPanel {

   public ProgramMap(AView Frame){
      
      setLayout(new GridLayout(0,4));
      //setPreferredSize(new Dimension(160, 100));
      this.setBackground(Color.WHITE);
      
      String P1 = "P1";
      new String[] { P2 }
      Course coreTemp = new CoreCourse(P1, "Programming 2", new String[] { P2 });

      addCorePanel(coreTemp);

   }
   public void addCorePanel(Course coreTemp){
      
      JButton corePanel = new JButton();
   
      corePanel.setLayout(new GridLayout(4,1));
      corePanel.setBackground(Color.GRAY);
      corePanel.setBorder(BorderFactory.createLineBorder(Color.black,3));
   
      JLabel coreCode = new JLabel("Code: " + coreTemp.getCode());
      JLabel coreTitle = new JLabel("Title: " + coreTemp.getTitle()); 
      JLabel coreCreditPoints = new JLabel("Credit Points: " + coreTemp.getCreditPoints());
      JLabel corePreReq = new JLabel("PreReq: ");
      //JLabel prLabel = new JLabel("Prereqs: ");
   
      // core panel properties
      corePanel.add(coreCode);
      corePanel.add(coreTitle);
      corePanel.add(coreCreditPoints);
      //corePanel.add(corePreReq);
      
      corePanel.setAlignmentX(LEFT_ALIGNMENT);
      corePanel.setAlignmentY(TOP_ALIGNMENT);

      //Needed to rebuild the prerequisites for display.
      String[] prereqs = null;
      String result = null;
      prereqs = coreTemp.getPreReqs();

      if (prereqs != null)
      {
         StringBuilder builder = new StringBuilder();
         for(String s : prereqs) 
         {
            builder.append(s);
            builder.append("\n");
         }
         result = builder.toString();
      }
       add(corePanel);
}




so what happens is if I make it static the method static is my

add(corePanel); // (JPanel)

for display fails
Cannot make a static reference to the non-static method add(Component) from the type Container

But because its a JButton I am not sure of a workaround

This post has been edited by floppyspace: 30 January 2013 - 09:43 AM

Was This Post Helpful? 0
  • +
  • -

#8 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

Re: Cannot make a static reference to the non-static method

Posted 30 January 2013 - 09:51 PM

I realised it was meant to be added to my Course Object...., was skipping a step by trying to directly add to my ProgramMap.

Thank you for the assistance once again.
Was This Post Helpful? 0
  • +
  • -

#9 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

Re: Cannot make a static reference to the non-static method

Posted 05 February 2013 - 02:55 AM

So I thought this was ok, but still have a problem : (

My data From the Listener is going to my ProgramMap, but for me to pass it too my addCorePanel I need to initialise my object with.

Course coreTemp = null;


So when it updates it is automatically taking the null value and wiping out the newl'y added object.

I have played around with this but have not been able to solve it...

What could it be that I am doing wrong when I pass the object back to my map?
Was This Post Helpful? 0
  • +
  • -

#10 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

Re: Cannot make a static reference to the non-static method

Posted 05 February 2013 - 03:22 AM

I think the problem is fixed as data is now going to my map ok, but it is still not updating my map so it can be displayed.

I tried

programMap.revalidate();

programMap.repaint();

programMap.validate();


from my listener class, these are now throwing a null pointer error on these methods, how can it refresh the programMap to display this new data?
Was This Post Helpful? 0
  • +
  • -

#11 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Cannot make a static reference to the non-static method

Posted 05 February 2013 - 02:56 PM

We're flying blind. Post updated code.
Was This Post Helpful? 0
  • +
  • -

#12 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

Re: Cannot make a static reference to the non-static method

Posted 06 February 2013 - 09:24 PM

my test line is telling me that it is now going to my map but I do not see any buttons showing, may be design issue but not sure I feel it should work but needs something....

ProgramMap
package ams.view;

import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import ams.model.*;
import ams.model.facade.AMSFacade;
import ams.model.facade.AMSModel;

public class ProgramMap extends JPanel {
   
   public static AMSModel model;
   
   private boolean hasInitialised = false;
   
   public ProgramMap(AMSView Frame){
          
      // initialise program
      if (hasInitialised == false)
      {
      initialiseEngine();
      hasInitialised = true;
      }
      
      setLayout(new GridLayout(0,4));
      //setPreferredSize(new Dimension(160, 100));
      this.setBackground(Color.WHITE);
      
      System.out.println(model.getAllCourses());
      System.out.println("here");
      
      
      //Test lines 
      //String P2 = "P1";
      //String P1 = "COSC1073";
      //Course coreTemp3 = new CoreCourse(P2, "Programming 2", new String[] { P1 });
      
      //String P12 = "P1";
      //Course coreTemp2 = new CoreCourse(P12, "Programming 1", null);
      
      //Course electtemp1 = new ElectiveCourse(P12, P12, 6, null);
      
      //addCorePanel(coreTemp2);
      
      //addCorePanel(coreTemp3);
      //addElectivePanel(electtemp1);
      
      //if(coreTemp != null)
      //{  
      //    addCorePanel(coreTemp);
      //}
      
   }



   public void addElectivePanel(Course coreTemp){
      
      JButton electivePanel = new JButton();
   
      electivePanel.setLayout(new GridLayout(4,1));
      electivePanel.setBackground(Color.GRAY);
      electivePanel.setBorder(BorderFactory.createLineBorder(Color.black,3));
   
      JLabel electiveCode = new JLabel("Code: " + coreTemp.getCode());
      JLabel electiveTitle = new JLabel("Title: " + coreTemp.getTitle()); 
      JLabel electiveCreditPoints = new JLabel("Credit Points: " + coreTemp.getCreditPoints());
      JLabel electivePreReq = new JLabel("PreReq: ");
      
      // core panel properties
      electivePanel.add(electiveCode);
      electivePanel.add(electiveTitle);
      electivePanel.add(electiveCreditPoints);
      electivePanel.add(electivePreReq);
      
      electivePanel.setAlignmentX(LEFT_ALIGNMENT);
      electivePanel.setAlignmentY(TOP_ALIGNMENT);

      //Needed to rebuild the prerequisites for display.
      String[] prereqs = null;
      String result = null;
      prereqs = coreTemp.getPreReqs();

      if (prereqs != null)
      {
         StringBuilder builder = new StringBuilder();
         for(String s : prereqs) 
         {
            builder.append(s);
            builder.append("\n");
         }
         result = builder.toString();
      }
       add(electivePanel);
}

   public static JButton addCorePanel(Course coreTemp){
           
      JButton corePanel = new JButton();
   
      corePanel.setLayout(new GridLayout(4,1));
      corePanel.setBackground(Color.GRAY);
      corePanel.setBorder(BorderFactory.createLineBorder(Color.black,3));
   
      JLabel coreCode = new JLabel("Code: " + coreTemp.getCode());
      JLabel coreTitle = new JLabel("Title: " + coreTemp.getTitle()); 
      JLabel coreCreditPoints = new JLabel("Credit Points: " + coreTemp.getCreditPoints());
      //JLabel corePreReq = new JLabel("PreReq: ");
   
      // core panel properties
      corePanel.add(coreCode);
      corePanel.add(coreTitle);
      corePanel.add(coreCreditPoints);
      //corePanel.add(corePreReq);
      
      corePanel.setAlignmentX(LEFT_ALIGNMENT);
      corePanel.setAlignmentY(TOP_ALIGNMENT);

      //Needed to rebuild the prerequisites for display.
      String[] prereqs = null;
      String result = null;
      prereqs = coreTemp.getPreReqs();

      if (prereqs != null)
      {
         StringBuilder builder = new StringBuilder();
         for(String s : prereqs) 
         {
            builder.append(s);
            builder.append("\n");
         }
         result = builder.toString();
      }
      //add(corePanel);
      return corePanel;

   }

   
   public void initialiseEngine() {
      model = new AMSFacade();
      model.addProgram(new Program("Code101", "Bachelor of Computer Science"));
      System.out.println("Initialised engine with Program "
         + model.getProgram().toString());
   }

}



AddCourseListener
package ams.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

import javax.swing.JOptionPane;
import ams.model.*;
import ams.model.exception.ProgramException;
import ams.view.*;

public class AddCourseListener implements ActionListener {

   private AMSView frame;
   private ProgramMap programMap;
   
   public AddCourseListener(AMSView frame) {
      this.frame = frame;
      this.programMap = programMap;
   }
   
   public void windowClosing(WindowEvent e) throws ProgramException {
      confirmAdd();
   }

   @Override
   public void actionPerformed(ActionEvent e) {
      try {
         confirmAdd();
      } catch (ProgramException e1) {
         // TODO Auto-generated catch block
         e1.printStackTrace();
      }

   }
   
   public void confirmAdd() throws ProgramException {
      
      // Check course name is 8 correct characters
      int message = 0;
      if (ToolBar.getCourseCodeInput().getText().length() != 8)
      {
         message = JOptionPane.PLAIN_MESSAGE;
         int type = JOptionPane.OK_OPTION;
      
         JOptionPane.showConfirmDialog
               (frame,"Course codes must be 8 characters only! ","Input Error",
            message,type);
      }
      
      // Check the title is greater than 2 characters
      else if(ToolBar.getCourseTitleInput().getText().length() < 2) 
      {
         message = JOptionPane.PLAIN_MESSAGE;
         int type = JOptionPane.OK_OPTION;
      
         JOptionPane.showConfirmDialog
               (frame,"Title must be greater than 2 characters! ", "Input Error",
            message,type);
      }
      
      else if (ToolBar.courseType.getSelectedItem().equals("Core"))
      {
         int confirmAdd = JOptionPane.showConfirmDialog(
               frame,
               "Do you wish to add this course?",
               "Add Course" +
               "",
               JOptionPane.YES_NO_OPTION);
         
         if(confirmAdd == JOptionPane.YES_OPTION)
         {
            
            Course coreTemp = new CoreCourse(
                  ToolBar.getCourseCodeInput().getText(),
                  ToolBar.getCourseTitleInput().getText(), 
                  null);
            
                  System.out.println(coreTemp.toString());
                  
                  ProgramMap.model.addCourse(coreTemp);//addCorePanel(coreTemp);
            
                  //programMap.validate();
                  //programMap.repaint();

            
            System.out.println("Added");
            
            System.out.print(ProgramMap.model.getAllCourses().toString());
         }
         
      }
      else {
         if(ToolBar.courseType.getSelectedItem().equals("Elective"))
         {
            int confirmAdd = JOptionPane.showConfirmDialog(
                  frame,
                  "Do you wish to add this course?",
                  "Add Course" +
                  "",
                  JOptionPane.YES_NO_OPTION);
            
            if(confirmAdd == JOptionPane.YES_OPTION)
            {
                  new ElectiveCourse(
                     ToolBar.getCourseCodeInput().getText(),
                     ToolBar.getCourseTitleInput().getText(), 
                     ToolBar.getCreditPoints());
                  
               //programMap.addCorePanel(coreTemp);

               //programMap.coreTemp = electiveTemp;
               
            }            
         } 
      }  
   }
         
   public void addActionListener(AddCourseListener addCourseListener) {
      try {
         confirmAdd();
      } catch (ProgramException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }  

}


The listener is sending the information to the ProgramMap but I am not handling it right to make the buttons display...

Thanks for your patience.
Was This Post Helpful? 0
  • +
  • -

#13 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: Cannot make a static reference to the non-static method

Posted 07 February 2013 - 07:41 PM

and in a static content you are not allowed to use the keyword "super" either
Was This Post Helpful? -1
  • +
  • -

Page 1 of 1