473,422 Members | 2,059 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,422 software developers and data experts.

Why am I getting a static reference error and how do I fix it?

I am getting the "Cannot make a static reference to the non-static method add(Component) from the type Container" error on the last line where I am adding the Welcome label. Why and how do I fix it?

Expand|Select|Wrap|Line Numbers
  1. //import statements
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. //class header with extends JPanel
  7. public class LoanComparison extends JPanel
  8. {
  9.  
  10.     /**
  11.      * 
  12.      */
  13.     private static final long serialVersionUID = 1L;
  14.     /**
  15.      * 
  16.      */
  17.     static JPanel mainPanel, centerPanel, bottomPanel;
  18.     static JLabel Welcome, loanAmount, years, rate, monthlypayment, totalpayment;
  19.     static JTextField loanTextField;
  20.  
  21.     public static void main (String[]args)
  22.     {
  23.  
  24.     //declare instance variables
  25.     int years, balance;
  26.     double loanAmount= 0, rate=0, monthlyrate=0, monthlypayment=0, totalpayment=0;
  27.  
  28.     //do following inside constructor method
  29.  
  30.         //set inherited Panel to BorderLayout
  31.         //from here onwards, I will reference to this panel as main panel
  32.         mainPanel.setLayout (new BorderLayout());
  33.         JFrame frame= new JFrame("Comparing Loans");
  34.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  35.  
  36.         //set preferred size for the main panel
  37.         mainPanel.setLayout (new GridLayout (1,2,5,10));
  38.  
  39.         //create a label to display welcome message
  40.         //add the above label to the main panel
  41.         JLabel Welcome = new JLabel ("Welcome to Compairing Loans Application");
  42.         add (Welcome);
Oct 28 '10 #1

✓ answered by Dheeraj Joshi

add is not a static method. So you need to access it using the object.

Expand|Select|Wrap|Line Numbers
  1. objname.method();
Regards
Dheeraj Joshi

3 2959
code green
1,726 Expert 1GB
Looks like add() is a class method, therefore the class object must be referenced to access add()
Oct 28 '10 #2
Dheeraj Joshi
1,123 Expert 1GB
add is not a static method. So you need to access it using the object.

Expand|Select|Wrap|Line Numbers
  1. objname.method();
Regards
Dheeraj Joshi
Oct 28 '10 #3
thank u sorted my headache :))
pweee..
cheers very helpful
Dec 7 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: NickA | last post by:
I'm getting the following error: '<name>' is declared in project '<projectname1>', which is not referenced by project '<projectname2>' It just started showing up the last time I did a build. ...
16
by: Paul S. Natanson | last post by:
What is a Null Reference error and how do I fix it? My newly installed VB.Net2003 gives me a "Microsoft Development Environment" error message box EVERY time I try to run/start ANY project -...
2
by: Jeff | last post by:
I'm getting an Object Reference error before I even run my app, and I'm not sure where to look to find the cause. I'd appreciate your help. When I open my Windows Application project, the...
2
by: lwoods | last post by:
I have the following function: function clean_form( &$from_check ) { if(is_array($from_check)){ array_walk(&$from_check,'clean_form'); return; } else { $value =...
8
by: utab | last post by:
Dear all, I am getting an undefined reference error wrn I try to initilialize a static map in the class constructor. my map: map<string,vector<string> > m; and in the constructor ...
6
by: David Lozzi | last post by:
Hello there, I'm getting the following error System.NullReferenceException: Object reference not set to an instance of an object. at shopping_bag.GetBagTotals()
1
by: prithvis.mohanty | last post by:
using System; using System.Drawing; using System.ComponentModel; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Text; using System.Collections; using...
6
by: Grey Alien | last post by:
class A { public: A(const B& ref); private: static B& b ; }; How may b be initialized ?
5
by: MattB | last post by:
I'm relatively familiar with asp.net and vb.net and have been working in the environment for several years. I've dealt with null reference errors before and generally know how to code around them...
9
Dököll
by: Dököll | last post by:
Does public static main encapsulate a class? Meaning: public static void main(String args) { // public class Myclass // code here to do other stuff
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.