Reputation: 13
Down in the second class named paintthis, in the constructor i declare that array = array.clone, and it says that there is nothing in the array? I declare the array in LineGraph and then put the array into the peramiters, which there is a constructor of the second class names painthis with the arguments int[] array.
package javatestframming;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class LineGraph extends JFrame{
public LineGraph(){
super("Line Graph");
int[] array = {1, 2, 3, 4, 5, 7, 8, 9};
setSize(400, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
paintthis gpane = new paintthis(array);
add(gpane);
setVisible(true);
}
public static void main(String[] args){
LineGraph lg = new LineGraph();
}
}
class paintthis extends JPanel{
int[] xpoints;
paintthis(int[] array){
xpoints = array.clone;
}
int max = arrayGetMaxInt(xpoints);
int min = arrayGetMinInt(xpoints);
int divisable = findDivisableWholeNumber(max, min);
Font f = new Font("Arial", Font.BOLD, 14);
FontMetrics fm = getFontMetrics(f);
public void paintComponent(Graphics g){
Graphics2D g2d = (Graphics2D) g;
super.paintComponents(g);
int graphHeight = 200;
int graphWidth = 200;
int graphX = 10;
int graphY = 10;
float borderThickness = 5.0f;
//set graph background and border
g2d.setColor(Color.white);
g2d.fillRect(graphX, graphY, graphWidth, graphHeight);
g2d.setColor(Color.GRAY);
BasicStroke bs = new BasicStroke(borderThickness, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
g2d.setStroke(bs);
g2d.drawLine(graphX - (int)borderThickness, graphY, graphX + graphWidth, graphY);
g2d.drawLine(graphX - (int)borderThickness, graphY + graphHeight, graphX + graphWidth, graphY + graphHeight);
g2d.drawLine(graphX - (int)borderThickness, graphY, graphX - (int)borderThickness, graphY + graphHeight);
g2d.drawLine(graphX + graphWidth, graphY, graphX + graphWidth, graphY + graphHeight);
int trueGraphWidth = graphWidth - drawYAxis(g2d, graphWidth, graphHeight, max, min, divisable, graphY, graphX);
int trueGraphHeight = graphHeight - drawXAxis(g2d, graphY + graphHeight - fm.getHeight(), graphX, graphWidth, (int)borderThickness);
System.out.println(trueGraphWidth + " " + graphWidth);
drawPoints(g2d, trueGraphWidth, trueGraphHeight, (graphWidth - trueGraphWidth) + (int)borderThickness + graphX, (graphHeight - trueGraphHeight) + (int)borderThickness + graphY);
}
public int drawYAxis(Graphics2D g, int graphWidth, int graphHeight, int max, int min, int counts, int beginingpoint, int overx){
graphHeight -= fm.getHeight();
int averageSpaceInBetween = ((graphHeight-fm.getHeight())/((max - min)/counts)); //- ((fm.getHeight()/(graphHeight/fm.getHeight())));
int space = 0;
Stack<Integer> stackAxisNumbers = new Stack<Integer>();
g.setFont(f);
for (int x = 0; x <= (max - min); x+=counts){
stackAxisNumbers.add(x + min);
}
for (int x = 0; x <= (max - min); x+= counts){
g.drawString("$" + Integer.toString(stackAxisNumbers.pop()), overx, space + beginingpoint + fm.getHeight());
space += averageSpaceInBetween;
}
space = 0;
return fm.stringWidth("$" + Integer.toString(max));
}
public int drawXAxis(Graphics2D g, int beginingpoint, int overx, int graphWidth, int borderThickness){
g.drawLine(overx - borderThickness, beginingpoint, overx + graphWidth, beginingpoint);
return beginingpoint - fm.getHeight();
}
public void drawPoints(Graphics2D g, int graphWidth,int graphHeight, int startX, int startY){
g.setColor(Color.blue);
int xSpace = (int) graphWidth/xpoints.length;
g.drawLine(startX, startY, xSpace + startX, xpoints[0]);
}
public int arrayGetMaxInt(int[] array){
int max = 0;
for (int x: array){
if (x > max){
max = x;
}
}
return max;
}
public int arrayGetMinInt(int[] array){
int min = max;
for (int x: array){
if (x < min){
min = x;
}
}
return min;
}
public int power(int base, int exp){
int num = base;
for (int x = 1; x < exp; x++){
num = num * base;
}
if (exp == 0){
num = 1;
}
return num;
}
public int findDivisableWholeNumber(int max, int min){
boolean passed = false;
int range = max - min;
int divis = 1;
int multiplier = 1;
out:
for (int x = 5; x > 0; x--){
if (range >= power(10, x)){
multiplier = x;
break;
}
}
out:
for (int x = 10 * multiplier; x <= 10 * multiplier && x > 0; x--){
if(range%((int)(range/x)) == 0){
divis = x;
passed = true;
break;
}
}
if (passed == false){
out:
for (int x = 11 * multiplier; x <= (int)(range / 2); x++){
if(range%((int)(range/x)) == 0){
divis = x;
passed = true;
break;
}
}
}
if (passed == false){
System.out.println("ERROR -- NoWholeDiviableNumbersException");
System.exit(0);
}
return divis;
}
}
Exception is:
Exception in thread "main" java.lang.NullPointerException
at javatestframming.paintthis.arrayGetMaxInt(LineGraph.java:97)
at javatestframming.paintthis.<init>(LineGraph.java:31)
at javatestframming.LineGraph.<init>(LineGraph.java:15)
at javatestframming.LineGraph.main(LineGraph.java:22)
C:\Users\Morgan Higginbotham\AppData\Local\NetBeans\Cache\8.1\executor- snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)
Upvotes: 0
Views: 80
Reputation: 9633
When you create an object using paintthis gpane = new paintthis(array);
It will initialize class level variables first
int max = arrayGetMaxInt(xpoints);
then body of constructor is called. In this case xpoints
is still not initialized, so you are getting NullPointerException
You can try some thing like this
class paintthis extends JPanel{
int[] xpoints;
int max;
int min;
int divisable;
public paintthis(int[] array){
xpoints = array.clone();
max = arrayGetMaxInt(xpoints);
min = arrayGetMinInt(xpoints);
divisable = findDivisableWholeNumber(max, min);
}
Upvotes: 1