如何修复空指针异常?

时间:2019-03-24 19:38:34

标签: java

家庭作业问题:

  

将以下构造函数添加到Line类:   公用线路(int x1,int y1,int x2,int y2)   构造包含两个给定点的新线。

我的代码:

主类:

"This browser doesnt support the API's required to use the firebase SDK. (messaging/unsupported-browser).

积分等级:

        //
        // Edit column for STUDENT_ID field
        //
        $editor = new ComboBox('student_id_edit', $this->GetLocalizerCaptions()->GetMessageString('PleaseSelect'));
        $lookupDataset = new TableDataset(
            SqlSrvConnectionFactory::getInstance(),
            GetConnectionOptions(),
            '[dbo].[tblStudent]');
        $lookupDataset->addFields(
            array(
                new IntegerField('STUDENT_ID', true, true, true),
                new StringField('FIRST_NAME', true),
                new StringField('LAST_NAME', true),
                new DateField('DOB', true),
                new StringField('POSTAL_CODE', true),
                new StringField('HOME_PHONE', true),
                new StringField('GENDER', true),
                new StringField('CURR_GRADE', true),
                new StringField('TIMETABLE_ID', true)
            )
        );

        $lookupDataset->setOrderByField('STUDENT_ID', 'ASC');
        $editColumn = new LookUpEditColumn(
            'STUDENT ID', 
            'STUDENT_ID', 
            $editor, 

            // The second 'STUDENT_ID' is what is displayed in the combo box
            $this->dataset, 'STUDENT_ID', 'STUDENT_ID', $lookupDataset);

        $validator = new RequiredValidator(StringUtils::Format($this->GetLocalizerCaptions()->GetMessageString('RequiredValidationMessage'), $editColumn->GetCaption()));
        $editor->GetValidatorCollection()->AddValidator($validator);
        $this->ApplyCommonColumnEditProperties($editColumn);
        $grid->AddInsertColumn($editColumn);

线路类别:

public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter x1: ");
        int x01 = scan.nextInt();
        System.out.println("Enter y1: ");
        int y01 = scan.nextInt();
        System.out.println("Enter x2: ");
        int x02 = scan.nextInt();
        System.out.println("Enter y2: ");
        int y02 = scan.nextInt();

        Line object2 = new Line(x01, y01, x02, y02);

        System.out.println(object2);

但是,我仍然收到空指针异常:

private int x;
    private int y;
    private static int count = 0;

    public Point(int x, int y){
        this.x = x;
        this.y = y;
        count++;
    }

    public Point(double x, int y){
        this.x = (int)x;
        this.y = y;
        count++;
    }

    public Point(){
        this(0,0); 
    }

    public String toString(){
        return "(" + x + ", " + y + ")";
    }

    public int getX(){
        return x;
    }

    public void setX(int newX){
        x = newX;
    }

    public int getY(){
        return y;
    }

    public void setY(int newY){
        y = newY;
    }

我应该收到

class Line {

   Point p1;
   Point p2;


    public Line(Point p1, Point p2) {
     this.p1=p1;
     this.p2=p2;

 }

       public Line(int x1, int y1, int x2, int y2) {

    Point p1 = new Point(x1, y1);
    Point p2 = new Point(x2, y2);

    }

    public Point getP1() {
        return p1;
    }
    public Point getP2() {
        return p2;
    }
    public String toString() {
        return "Line points [ (" +p1.getX() + ", "  + p1.getY() + ") , ("+p2.getX() + ", " + p2.getY() + ")]";

}

作为我的输出。这是什么问题?

0 个答案:

没有答案