0

I'm making a graphing calculator because my comp sci teacher said if i can prove i know all the topics that will be taught in the class he'll pass me for the rest of the year, so my idea is a graphing calculator, and I'm trying to put my applet mumbo jumbo in a different class. But after what i feel is doing things properly, i get an error when trying to use a method from my class with the applet. Both of these classes are in same package. Here's the class i'm calling from: (Also I imported java.awt.* already)

public class NumberCalc {
//Bunch of variable for calculator
Graphics page;
static GraphingCalc graph = new GraphingCalc();

/* Blah blah calculator shit for miles
*  main method is in here with the drawGraph method
*
*/

public static void drawGraph() {
    graph.addPoint(20,20,5,5);
    //Test method to see if it draws, next class has the info
    }
}

And here is the class I'm calling to:

public class GraphingCalc {
Graphics page;
//Code that doesnt matter right now
public void addPoint(int x, int y, int sizeA, int sizeB) {
    setBackground(color.black);
    page.setColor(color.cyan) //Easy to see color for testing
    page.fillOval(x,y,sizeA,sizeB);
    }    
}

And the error i get thrown is this:

Exception in thread main java.lang.NullPointerException
at calculator.GraphingCalc.addPoint(GraphingCalc:18)

line 18 is page.setColor(color.cyan), and if i comment out this line and go to the fillOval, then it gives the same error, so there is obviously something wrong with the 'page' graphics variable. Any solutions?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
JakeTheSnake
  • 372
  • 1
  • 11
  • See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Oct 21 '17 at 02:30
  • *"if i can prove i know all the topics that will be taught in the class he'll pass me for the rest of the year"* You apparently don't. In fact, you don't seem to know some of the 'Java 101' basics needed to approach GUI development. – Andrew Thompson Oct 21 '17 at 02:33
  • This is a sophomore class, this isnt being taught in the textbook he handed us. – JakeTheSnake Oct 21 '17 at 16:07

1 Answers1

1

A NullPointerException means that you are trying to refer to a variable which doesn't have a value. Here, you have a declaration of a field of type Graphics and the name page, but the field is never initialized (at least not in the code you posted). In other words - you're trying to ask the page to setColor(...), but there is no page (because you never provided it).

Piotr Wilkin
  • 3,446
  • 10
  • 18
  • Okay, i see this now thank you, but how would i go about doing that? – JakeTheSnake Oct 20 '17 at 21:52
  • 1
    To be honest, the answer is widely beyond what a simple answer here can give you. You need to learn the Swing library and how to write graphic programs with it. For an introductory course, see https://docs.oracle.com/javase/tutorial/uiswing/start/index.html For 2D graphics specifically, see https://docs.oracle.com/javase/tutorial/2d/overview/index.html – Piotr Wilkin Oct 20 '17 at 21:55