nullpointerexception arrray = array.clone不工作?

时间:2016-08-21 23:16:08

标签: java

在名为paintthis的第二个类中,在构造函数中我声明了array = array.clone,它说数组中什么都没有?我在LineGraph中声明了数组,然后将数组放入peramiters中,其中有第二个类的构造函数painthis,其参数为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 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(总时间:1秒)

1 个答案:

答案 0 :(得分:1)

使用int max = arrayGetMaxInt(xpoints); 创建对象时,它将首先初始化类级别变量

xpoints

然后调用构造函数的主体。在这种情况下,NullPointerException仍未初始化,因此您将获得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); }

你可以试试这样的事情

Access.key!