Jump to content

Java NullPointerException

oliverjrose99

Hi

 

when i run the code i get a "Null Pointer Exception" error. I Googled it but didn't understand it so what needs to be changed. thanks

import java.awt.Button;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JFrame;public class Gui implements ActionListener{		public Button b1;	public Button b2;		public static void main(String[] agrs) {		Gui gui = new Gui();	}		public Gui() {		JFrame frame = new JFrame();		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		frame.setSize(500, 500);		frame.setTitle("This is a title");		frame.setLocationRelativeTo(null);		frame.setLayout(null);		makeButton(b1, "Button 1", 10, 10, 200, 50);		makeButton(b2, "Button 2", 220, 70, 200, 50);				frame.add(b1);		frame.add(b2);		frame.setVisible(true);	}		public void makeButton(Button b, String label, int x, int y, int w, int h) {		b = new Button();		b.setLabel(label);		b.setBounds(x, y, w, h);		b.addActionListener(this);	}	@[member=override2299]	public void actionPerformed(ActionEvent event) {		if(event.getSource() == b1) {			System.out.println("Button 1 was pressed");		} else if(event.getSource() == b2) {			System.out.println("Button 2 was pressed");		}	}}
Link to comment
Share on other sites

Link to post
Share on other sites

null reference is when you are trying to call a method on a non initialised pointer (variable in this case)

at what point do you get the null pointer exception?

 

What I can think from this code at the moment is that your makeButton method is just making a copy of the button you are passing in, and initialising that, then you are using the non initialised version for everything else, athough it should be passing by reference, in which case calling new should work.

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

i'm not a java master, but i guess the problem is with buttons

 

it's about pointers and stuff: everytime you declare an Object in java, you create it somewhere in the RAM, and you save the pointer to that RAM location in your variable

when you call a function passing an Object as a paramenter, you pass the reference to the object

in this case, you execute

        makeButton(b1, "Button 1", 10, 10, 200, 50);        makeButton(b2, "Button 2", 220, 70, 200, 50);

but b1 and b2 are not initialized, so you're executing

        makeButton(null, "Button 1", 10, 10, 200, 50);        makeButton(null, "Button 2", 220, 70, 200, 50);

makeButton(), however, just doesn't care about what you pass as a parameter, because anyway it will create another object with

		b = new Button();

it will work with that object, and at the end of the function call it will just throw it away

 

so, this is why it doesn't work, to fix it you have to create the two Button objects somewhere else in the code, like in the Gui() and not call any "new" in the makeButton()

Link to comment
Share on other sites

Link to post
Share on other sites

i fixed it but now if i click the buttons it gives an error that "b1 and b2 can not be resolved to a variable" but before i made makeButton() i worked, how do i fix this as well

Link to comment
Share on other sites

Link to post
Share on other sites

i never did anything visual with java, so i don't know how it works

 

anyway, i see you do a

Gui gui = new Gui();

but after the main is executed, i think that the variable "gui" is erased, and since the object that it referred doesn't have any reference to it anymore, the garbage collector maybe deletes it and you end up losing some data

try putting "gui" as another private class field

 

and what do you mean by "but before i made makeButton() i worked"? can you post some new code?

Link to comment
Share on other sites

Link to post
Share on other sites

i never did anything visual with java, so i don't know how it works

 

anyway, i see you do a

Gui gui = new Gui();
but after the main is executed, i think that the variable "gui" is erased, and since the object that it referred doesn't have any reference to it anymore, the garbage collector maybe deletes it and you end up losing some data

try putting "gui" as another private class field

 

and what do you mean by "but before i made makeButton() i worked"? can you post some new code?

That's just how java gui's work. It's fine.

 

Also the previous one doesn't actually pass null in, it passes a variable with the value "null" which is then named as "b" in the method and initialised with the button constructor. But i think you're right that it's thrown away. I'm just used to explicit memory management with pointers after working with c++ for a few months :P

 

 

Try this:

public Button makeButton(String label, int x, int y, int w, int h) {		b = new Button();		b.setLabel(label);		b.setBounds(x, y, w, h);		b.addActionListener(this);                return b;}
and then

b1 = makeButton("Button 1", 10, 10, 200, 50);b2 = makeButton("Button 2", 220, 70, 200, 50);
Alternatively i think there is an "out" option which you can attatch to a parameter in a constructor which specifies that it is written to specifically and not thrown away, but that may just be c#

edit- "You can only pass by value in Java. Even references are passed by value"

this means that whatever you pass in to a function will be copied to a new object. You need to use the return type of the function to get an output otherwise what you use will be lost.

It's quite confusing since c# does everything by reference apart from primitives :P

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

Also the previous one doesn't actually pass null in, it passes a variable with the value "null" which is then named as "b" in the method and initialised with the button constructor. But i think you're right that it's thrown away. I'm just used to explicit memory management with pointers after working with c++ for a few months :P

mmm are you sure?

i think there is the variable "b1" which has content "null"

at function call, the value of "b1" gets copied into "b", but notice that it's not the same variable renamed, it's just its content which gets copied

"b" points at the new "Button" object

"b" dies when its environment dies (end of the function call)

 

edit:

also, about

 

this means that whatever you pass in to a function will be copied to a new object

actually you pass the reference to the object, so you work with the same object

with java you have to breakdance your way through pointers and references and values and object and shit, to understand what's going on behind the scenes

Link to comment
Share on other sites

Link to post
Share on other sites

mmm are you sure?

i think there is the variable "b1" which has content "null"

at function call, the value of "b1" gets copied into "b", but notice that it's not the same variable renamed, it's just its content which gets copied

"b" points at the new "Button" object

"b" dies when its environment dies (end of the function call)

 

edit:

also, about

actually you pass the reference to the object, so you work with the same object

with java you have to breakdance your way through pointers and references and values and object and shit, to understand what's going on behind the scenes

you seem to be contradicting yourself there :P

Passing by value is making a copy of the object in a different place in memory and giving it the same value, and passing by reference is modifying the object that you passed in.

you explained passing by value in your first part but then said that java passes by reference :P

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

you seem to be contradicting yourself there :P

Passing by value is making a copy of the object in a different place in memory and giving it the same value, and passing by reference is modifying the object that you passed in.

you explained passing by value in your first part but then said that java passes by reference :P

yep, java only passes by value

but objects are stored as references

so you pass parameters as reference values, which is equivalent as passing it by reference

Link to comment
Share on other sites

Link to post
Share on other sites

yep, java only passes by value

but objects are stored as references

so you pass parameters as reference values, which is equivalent as passing it by reference

Ah yeah, that sounds right.

Either way, the code i posted should solve the issue ;)

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×