6 Replies - 1772 Views - Last Post: 20 October 2013 - 10:34 PM Rate Topic: -----

#1 AndreeU17   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 163
  • Joined: 29-June 13

Still struggling with this Calculator!

Posted 20 October 2013 - 10:18 PM

This is the code i have as of now...
public class jCalculator extends JFrame{

	private JButton[] buttonArray;
	JPanel textareaPanel,buttonPanel;
	
	public static void main(String[] args){
		jCalculator cal = new jCalculator();
		cal.setVisible(true);
	}

	public jCalculator(){
		this.setTitle("jCalculator");
		this.setSize(250,300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		BorderLayout border = new BorderLayout();
		this.setLayout(border);
		
		FlowLayout flow = new FlowLayout();
		textareaPanel = new JPanel();
		textareaPanel.setLayout(flow);;
		JTextArea textarea = new JTextArea();
		textarea.setSize(240, 50);
		textareaPanel.add(textarea);
		this.add(textareaPanel, BorderLayout.NORTH);
		
		GridLayout grid = new GridLayout(4,4);
		buttonPanel = new JPanel();
		buttonPanel.setLayout(grid);
		for(int i=0;i<16;++i){
			buttonArray[i] = new JButton(); //this is line 43
			buttonPanel.add(buttonArray[i],BorderLayout.CENTER);
		}
		this.add(buttonPanel);
		
	}
}



I keep getting this error

Exception in thread "main" java.lang.NullPointerException
	at jCalculator.<init>(jCalculator.java:43)
	at jCalculator.main(jCalculator.java:18)



I have no idea what im doing wrong? Could any emphasize what i have wrote that lead to a problem! Also is the way im doing it efficient for a beginner ? The reason i'm trying to create objects for everything is so i become more accustom and well its always better to show off all your work instead of cheating myself out (Helps a beginner get better)!!!

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Still struggling with this Calculator!

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12683
  • View blog
  • Posts: 45,866
  • Joined: 27-December 08

Re: Still struggling with this Calculator!

Posted 20 October 2013 - 10:23 PM

You never initialized your array. You need to initialize: buttonArray = new JButton[someLength];.

By the way- not to be rude, but I would strongly encourage you to go back and learn the basics. You will pay for it with Swing if you try to get by without a strong grasp on the basics, including and especially OOP.
Was This Post Helpful? 0
  • +
  • -

#3 AndreeU17   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 163
  • Joined: 29-June 13

Re: Still struggling with this Calculator!

Posted 20 October 2013 - 10:26 PM

View Postmacosxnerd101, on 20 October 2013 - 11:23 PM, said:

You never initialized your array. You need to initialize: buttonArray = new JButton[someLength];.

By the way- not to be rude, but I would strongly encourage you to go back and learn the basics. You will pay for it with Swing if you try to get by without a strong grasp on the basics, including and especially OOP.


Yeah your right I should get a better grasp but I can't seem to find a decent book to help me sufficiently! Would you recommend Core Java Fundamental 9th edition to base my basics off or you think I should look for another book as resources/reference?

Also I believe I got the array part from the calculator tutorial here on DIC. I got it from him that's why I thought it was correct!

This post has been edited by AndreeU17: 20 October 2013 - 10:28 PM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12683
  • View blog
  • Posts: 45,866
  • Joined: 27-December 08

Re: Still struggling with this Calculator!

Posted 20 October 2013 - 10:28 PM

I haven't used a ton of books, personally. I picked up most of my OOP from going through people's questions in the forums and trying to solve their problems. If you want a book, there is a pinned book thread. Check there.
Was This Post Helpful? 0
  • +
  • -

#5 AndreeU17   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 163
  • Joined: 29-June 13

Re: Still struggling with this Calculator!

Posted 20 October 2013 - 10:30 PM

View Postmacosxnerd101, on 20 October 2013 - 11:28 PM, said:

I haven't used a ton of books, personally. I picked up most of my OOP from going through people's questions in the forums and trying to solve their problems. If you want a book, there is a pinned book thread. Check there.

Okay well thank you I know some basic but I should really master it so hopefully I can thanks for the quick tip
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12683
  • View blog
  • Posts: 45,866
  • Joined: 27-December 08

Re: Still struggling with this Calculator!

Posted 20 October 2013 - 10:32 PM

Quote

Also I believe I got the array part from the calculator tutorial here on DIC. I got it from him that's why I thought it was correct!

Neither schutzzz's post nor his tutorial indicated that the syntax you are defending was legal. In both cases, the array was initialized.
Was This Post Helpful? 0
  • +
  • -

#7 AndreeU17   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 163
  • Joined: 29-June 13

Re: Still struggling with this Calculator!

Posted 20 October 2013 - 10:34 PM

View Postmacosxnerd101, on 20 October 2013 - 11:32 PM, said:

Quote

Also I believe I got the array part from the calculator tutorial here on DIC. I got it from him that's why I thought it was correct!

Neither schutzzz's post nor his tutorial indicated that the syntax you are defending was legal. In both cases, the array was initialized.

Im sorry, I though I did it like his (using his tut as reference). I guess I got side tracked and tried to do it out of knowledge but Okay It seems im lacking a few things here and there. Thanks for the help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1