Please help me with JAVA... someone? - HotUKDeals
We use cookie files to improve site functionality and personalisation. By continuing to use HUKD, you accept our cookie and privacy policy.
Get the HUKD app free at Google Play

Search Error

An error occurred when searching, please try again!

Login / Sign UpSubmit

Please help me with JAVA... someone?

jamhops Avatar
6y, 7m agoPosted 6 years, 7 months ago
I am wondering if anyone can help me fix my java? If anyone can i will send or post the code but i am trying to run a method from one class to another and have:

'non static method can not be refrenced from a static context'

Searching google i kinda know what it means but anything i try doesnt fix it....


anyone please
jamhops Avatar
6y, 7m agoPosted 6 years, 7 months ago
Options

All Comments

(40) Jump to unreadPost a comment
Comments/page:
#1
Haven't done Java in a while... If you post it, could look at it but no promises.

Depends how long a class too, need to work on my own assignment -.- When you're trying to compile it, does it not give you a line number? Can you not see roughly where it is falling over?

Just by reading the error message, it looks like you're trying to call an object that doesn't exist yet. Check your spelling of everything, it's very possible you've wrongly named something, perhaps missing a capital letter or something.

http://stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static-c
#2
Basically, whatever method you're calling that throws that error, (stack trace will tell you), make it static :)
#3
razord
Basically, whatever method you're calling that throws that error, (stack trace will tell you), make it static :)


http://java.sun.com/developer/technicalArticles/Programming/Stacktrace/
#4


It should be throwing a stack trace already :) just paste it here..
#5
razord
It should be throwing a stack trace already :) just paste it here..


Thought it might help him anyway ;)
#6
Main-

public class Main{

double totalarea = 0.0;
double totalvolume = 0.0;
double glasscost = 0.0;
double shadingcost = 0.0;
double securitycost = 0.0;
double reflectivecost = 0.0;
boolean shading;
double thickness =0.0;



public Main(){
windowGUI.setVisible(true);
type1.option1();



}



public void TypeSelection(){
if (thickness >=.5 && thickness <1){
System.out.println("type 21");



}
if (thickness >=1 && thickness <2){
System.out.println("type 22");


}
if (thickness >=2 && thickness <=3){
System.out.println("type 23");
}
else {
System.out.println("Invalid Data");
}
}
#7
GUI (part 1)

public class Windows_GUI extends javax.swing.JFrame implements ActionListener{

boolean warned = false;
boolean checkThickness = false;
boolean checkArea = false;
double width, length, thickness;
public boolean plastic = false;
public boolean wood = false;
public boolean metal = false;
private Main mainClass;


/** Creates new form Windows_GUI */
public Windows_GUI(Main mainClass) {
this.mainClass = mainClass;
initComponents();
init();
}

// gets text from the blank textbox and assigns it to varia
public void getValues(){
try{
length =Float.parseFloat(jTextField2.getText());
width =Float.parseFloat(jTextField1.getText());
thickness =Float.parseFloat(jTextField3.getText());


}catch(NumberFormatException NumCheck){
JOptionPane.showMessageDialog(this,"Please check the input values");
NumCheck.printStackTrace();
}

}
#8
GUI (part 2)

public void setWindowWidth(double width)
{
this.width = width;
}

public void setWindowLength(double length)
{
this.length = length;
}

public void setWindowThickness(double thickness)
{
this.thickness = thickness;
}

public double getWindowWidth()
{
return width;
}

public double getWindowLength()
{
return length;
}

public double getWindowThickness()
{
return thickness;
}

}
#9
missed the middle of the GUI out its validation and the GUI design code,

My problems are the transfering data between the 2 and calling methods from other classes if some wise person can teach me (sorry for the delay)

(i declared thickness in Main but really want to get it from the gui with Windows_GUI.thickness
(something obvious i am guessing?)

EDIT: oh and also calling the selection system on GUI button (got the button just need the code that starts the method as Main.TypeSelection.. doesnt work)

oh and also will send the whole package to anyone who can help.. im using net beans
#10
anyone?
#11
ok hope is failing anyone like java or to persaude me its not pointless effort to try
#12
jamhops
ok hope is failing anyone like java or to persaude me its not pointless effort to try


It;s been a while since I did Java, but only really had a basic exposure. I can sort of see what's going on there but am really of no use.

Java is good though and pretty simple. Debugging has to be done unfortunately, such is the joy of programming. Were you not testing it along the way? Surely this would have only started falling over recently, so examine the last changes you made.

My memory is hazy, but in the GUI2 section, is it correct to be saying:

public void setWindowWidth(double width)
{
this.width = width;
}

?

I would have thought simply putting "width" in would have sufficed...
#13
DragonChris
It;s been a while since I did Java, but only really had a basic exposure. I can sort of see what's going on there but am really of no use.

Java is good though and pretty simple. Debugging has to be done unfortunately, such is the joy of programming. Were you not testing it along the way? Surely this would have only started falling over recently, so examine the last changes you made.

My memory is hazy, but in the GUI2 section, is it correct to be saying:

public void setWindowWidth(double width)
{
this.width = width;
}

?

I would have thought simply putting "width" in would have sufficed...


I am unsure that was what i was instructed but i think if i remember correctly its this width is being set to a alternate width as java does not declaring itself...
#14
jamhops
I am unsure that was what i was instructed but i think if i remember correctly its this width is being set to a alternate width as java does not declaring itself...


Damned if I know then sorry :p
#15
DragonChris
Damned if I know then sorry :p


Do you know how to get those values set with this in the main class... thought it was get.thickness = thickness but seems i was wrong?
#16
Which line of the code is causing the error?
#17
the GUI all works i can not work out how to get the data that the GUI collects into the main class so that it can be added, multiplied etc.

So in the if statement (TypeSelection) i can not the the thickness into the main doc.? Also i try and add a line of code onto button 2 (in GUI) so that on button click it runs the TypeSelection (the if statement in main) but i can not work out how?

Does that make any sense? So there arent really errors as i got annoyed and deleted anything red but still cant call methods or transfer data between classes? So like i said what i said make any sense :s
#18
If you paste it to pastebin or somewhere else where it keeps the formatting and colors the syntax, I'll take a look at it :)
#19
InfernoZeus
If you paste it to pastebin or somewhere else where it keeps the formatting and colors the syntax, I'll take a look at it :)


ok one sec :thumbsup:
banned#20
make the class that contains the method you are calling static, if it is a helper class.

OR instantiate the class that you are calling the method from first.

eg

HelperClass hc = new HelperClass();

String result = hc.methodNameThatIWantFromThisClass();
#22
bykergrove
make the class that contains the method you are calling static, if it is a helper class.

OR instantiate the class that you are calling the method from first.

eg

HelperClass hc = new HelperClass();

String result = hc.methodNameThatIWantFromThisClass();


I know i am a complete idiot but in my context i do it how? :?
#23
anyone?
#24
I am a developer by profession and without looking at your code I believe you are trying to call a method in a class before an object of that class has been instantiated. You cannot call a method if the object has not been created. You can call a static method of a class though because that means the static method exists statically rather than dynamically, hence an object of that class does not need to be created.

So we need to establish several things:

1. Are you trying to call a method of a class whereby the method exclusively belongs to the class (and not an instantiated object). If so, make the method static and you can call it without instantiating the object.
2. If you are calling a method which depends on a class object, then you should not make the method static but you MUST instantiate the object and use the DOT ACCESSOR of the OBJECT (NOT THE CLASS) to call the method.
#25
jamhops
anyone?

Which line is the problem at?
#26
ElliottC
I am a developer by profession and without looking at your code I believe you are trying to call a method in a class before an object of that class has been instantiated. You cannot call a method if the object has not been created. You can call a static method of a class though because that means the static method exists statically rather than dynamically, hence an object of that class does not need to be created.

So we need to establish several things:

1. Are you trying to call a method of a class whereby the method exclusively belongs to the class (and not an instantiated object). If so, make the method static and you can call it without instantiating the object.
2. If you are calling a method which depends on a class object, then you should not make the method static but you MUST instantiate the object and use the DOT ACCESSOR of the OBJECT (NOT THE CLASS) to call the method.


The code is above and i tried to make it static and many other thing but think i may be being stupid and cant get it to work...
but yes i think its trying to get the values/class before it has been initiated and i think yes to 2 :s but have not idea on that answer is there any chance u can browse the code above... the major bit are top and bottom GUI (middle is the netbeans auto design tool) and method and type 1 should be easy to understand for anyone which can do java... which isnt me :oops:

If you can look then thanks for the help and anything u can assist would be great as i am*** to present it tomorrow...
#27
InfernoZeus
Which line is the problem at?


Well the line that reads windowGUI.SetVisible makes a call without an object having been instantiated (unless I have missed a line where windowGUI has been defined or the line has not been included in the listing). The compiler would therefore deem it impossible to call the SetVisible method since it is not a static method.
#28
jamhops
The code is above and i tried to make it static and many other thing but think i may be being stupid and cant get it to work...
but yes i think its trying to get the values/class before it has been initiated and i think yes to 2 :s but have not idea on that answer is there any chance u can browse the code above... the major bit are top and bottom GUI (middle is the netbeans auto design tool) and method and type 1 should be easy to understand for anyone which can do java... which isnt me :oops:

If you can look then thanks for the help and anything u can assist would be great as i am*** to present it tomorrow...


You made a call to windowGUI.SetVisible. I cannot see where you have instantiated windowGUI. I need to know what your intentions were. Did you intend to call SetVisble on a class called windowGUI (in which case, you have a class called Windows_GUI) or did you intend to create an object windowGUI and have forgotten to do so?
#29
I'm just importing the files into Eclipse, are those the only files?
#30
InfernoZeus
Which line is the problem at?


GUI
284
372,

Main
28-31,
37-45 (where ever it mentions thickness as i cant work out how to get it from GUI)

:(
#31
If you intend to call a method of an object here is an example:

Firstly, you need to create the class such as

public class MyPoint
{
public int x = 0;
public int y = 0;

public Point(int iPointX, int iPointY)
{
x = iPointX;
y = iPointY;
}

public SetPoint(int iPoint, int iPointY)
{
x = iPointX;
y = iPointY;
}

}

Now in order to call the method SetPoint you cannot call it directly without instantiating an object because it is NOT a static method. If it was static you could call MyPoint.SetPoint but as it is not static you should create an object first, like so:

Point somePoint = new MyPoint(1, 2);

Then call the method like so:

somePoint.SetPoint(50, 100);
#32
ElliottC
You made a call to windowGUI.SetVisible. I cannot see where you have instantiated windowGUI. I need to know what your intentions were. Did you intend to call SetVisble on a class called windowGUI (in which case, you have a class called Windows_GUI) or did you intend to create an object windowGUI and have forgotten to do so?


ok i set the GUI to visible it pops up i input the width, thickness, height and it processes the data and once it has got the extras from the user (which are not collected atm) it is supposed to send the data back to main so it can process it to work out the total of a window...
#33
jamhops
GUI
284
372,

Main
28-31,
37-45 (where ever it mentions thickness as i cant work out how to get it from GUI)

:(


Define your global variables in a class!
#34
Ok, I got it working, do you want me to just paste the 3 files into pastebin? or shall I just PM you what needs changing?
#35
ElliottC
If you intend to call a method of an object here is an example:

Firstly, you need to create the class such as

public class MyPoint
{
public int x = 0;
public int y = 0;

public Point(int iPointX, int iPointY)
{
x = iPointX;
y = iPointY;
}

public SetPoint(int iPoint, int iPointY)
{
x = iPointX;
y = iPointY;
}

}

Now in order to call the method SetPoint you cannot call it directly without instantiating an object because it is NOT a static method. If it was static you could call MyPoint.SetPoint but as it is not static you should create an object first, like so:

Point somePoint = new MyPoint(1, 2);

Then call the method like so:

somePoint.SetPoint(50, 100);


it doesnt like
public Point(int iPointX, int iPointY)

(sorry i work on reverse engineering to learn things :roll:
#36
InfernoZeus
Ok, I got it working, do you want me to just paste the 3 files into pastebin? or shall I just PM you what needs changing?


both if you dont mind... :thumbsup:

want to understand it and get it working... so that i can add the other bits to it for the finished product
#37
ElliottC
Define your global variables in a class!


so calss global(thickness....){
double thickness....
}
;

?
#38
jamhops
it doesnt like
public Point(int iPointX, int iPointY)

(sorry i work on reverse engineering to learn things :roll:


Obviously it should be public class Point - I just typed it in quickly but well spotted
#39
jamhops
so calss global(thickness....){
double thickness....
}
;

?


Yep, because Java does not support Global variables. I'm not sure how you got away with it unless you are using Microsoft's non-standard implementation.

Anyway to pass parameters (I believe that is what you need to do to pass the thickness), parameter passing in Java is by value - ALWAYS. What you can do is pass the parameter as a class and call a method within the class to set the member variables. Did I just recommend not to use glbal variables and use a class?? How convenient. As you can store thickness in a class, you pass the class as a parameter then call a mtehod in that class to set the member variable, thickness and have a method to get the member variable thickness out.

Unfortunately, I can't correct your errors but I hope I have provided enough pointers for you to realise what the issues are.
#40
jamhops
ok i set the GUI to visible it pops up i input the width, thickness, height and it processes the data and once it has got the extras from the user (which are not collected atm) it is supposed to send the data back to main so it can process it to work out the total of a window...


I'm still not sure where you defined windowGUI.

Post a Comment

You don't need an account to leave a comment. Just enter your email address. We'll keep it private.

...OR log in with your social account

...OR comment using your social account

Thanks for your comment! Keep it up!
We just need to have a quick look and it will be live soon.
The community is happy to hear your opinion! Keep contributing!