为什么我在处理文本字段时遇到问题?

时间:2016-09-08 07:56:15

标签: java arrays javafx nullpointerexception

当我尝试将值分配给TextField数组时,我一直收到异常。为什么?对于普通数组,它的工作原理如此,为什么它不适用于TextField数组?

CODE

import javafx.scene.paint.Paint;
import javafx.scene.paint.Color;
import javafx.scene.layout.GridPane;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.text.Text;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import java.lang.String;
import javafx.scene.control.TextField;
import java.lang.*;
import javafx.scene.Node;
import javafx.scene.paint.Paint;
import javafx.scene.paint.Color;
import javafx.scene.control.Button;
import javafx.event.EventHandler;

public class Assignment22_19 extends Application{
/*
I got the the findLargestBlock method from 
https://github.com/mission-peace/interview/blob/master/src/com/interview/dynamic/MaximumSizeSubMatrix.java
However he copied it from http://www.geeksforgeeks.org/maximum-size-sub-matrix-with-all-1s-in-a-binary-matrix/
He modified it to be more concise, and then I modified it to be even more concise.

*/
    static TextField arr[][] = new TextField[10][10];
    static int x;
    static int y;
    static GridPane binaryPane = new GridPane();

    @Override
    public void start(Stage primaryStage){
        Text one = new Text("1");
        Text zero = new Text("0");
        arr = generateArray();
        int biggestBlockSize = findLargestBlock();

        for (int i = 0; i < arr.length; i++){
            for(int j =0; j < arr[0].length; j++){
                //binaryPane.add(new TextField(arr[i][j] + ""), i, j);
            }
        }


        Button findLargestBlockButton = new Button("Click Me");
        binaryPane.add(findLargestBlockButton, 5, 11);
        findLargestBlockButton.setOnAction(ov -> {
             System.out.println(findLargestBlock());
        });

        Scene scene = new Scene(binaryPane, 250, 280);
        primaryStage.setTitle("Assignment 22.19"); // Set the stage title
        primaryStage.setScene(scene); // Place the scene in the stage
        primaryStage.show(); // Display the stage
    }
    //Makes a random array    
    public TextField[][] generateArray(){
        TextField temp[][] = new TextField[10][10];

        for (int i = 0; i < arr.length; i++){
            for(int j = 0; j < arr[0].length; j++){
                temp[i][j].setText((int)Math.floor(Math.random() * 2) + "");
                System.out.print( temp[i][j] + "");
            }
        }

        return temp;
    }

    public int findLargestBlock(){
        TextField result[][] = new TextField[arr.length][arr[0].length];
        //copies arr to reult
        for(int i = 0; i < arr.length; i++){
            result[i][0] = arr[i][0];
        }
        //same
        for(int i = 0; i < arr[0].length; i++){
            result[0][i] = arr[0][i];
        }

        int max = 1;
        //calculate largest block
        for(int i = 1; i < arr.length; i++){
            for(int j = 1; j < arr[i].length; j++){
                if(Integer.parseInt(arr[i][j].getText()) == 0){
                    continue;
                }
                int t = Math.min(Integer.parseInt(result[i-1][j].getText()), Math.min(Integer.parseInt(result[i-1][j-1].getText()),Integer.parseInt(result[i][j-1].getText())));
                result[i][j].setText((t + 1) + "");
                if(Integer.parseInt(result[i][j].getText()) > max){
                    x = i - max;
                    y = j - max;
                    max = Integer.parseInt(result[i][j].getText());
                }
            }
        }
        //color largest block
        if (max >= 1){
            for (int i = 0; i < max; i++){
                for(int j =0; j < max; j++){
                   // getNodeFromGridPane(binaryPane, x + 1, y + 1).setStyle("-fx-background-color: black;");
                }
            }
        }
            //console testers
        System.out.print(max);
        System.out.println("\n" + x);
        System.out.println(y);
        return max;
    }
    //I got this part from StackOverflow (Shreyas Dave: author)
    private Node getNodeFromGridPane(GridPane gridPane, int col, int row) {
        for (Node node : gridPane.getChildren()) {
            if (GridPane.getColumnIndex(node) == col && GridPane.getRowIndex(node) == row) {
                return node;
            }
        }
        return null;
    }


    /*public static void main(String args[]){
    /*
                ___      |\________/)
     'Sup      [_,_])    \ /       \|
              /|=T=|]     /   __  __\
              |\ " //     |_  9   p ]\
              ||'-'/--.  / /\ =|  \|\ \
             /|| <\/> |\ | '._, @ @)<_)
            | |\   |  |   \.__/(_;_)
            |  .   H  |   |  :  '='|
            |  |  _H__/  _| :      |
             \  '.__  \ /  ;      ';
            __'-._(_}==.'  :       ;
           (___|    /-' |   :.     :
          [.-'  \   |   \   \ ;   :
         .-'     |  |    |  |   ":
        /        |==|     \  \  /  \_
       /         [  |      '._\_ -._ \
      /           \__)   __.- \ \   )\\
     /       |        /.'      >>)
     |        \       |\     |
     |     .'  '-.    | \    /
     |    /     /     / /   /
                |    / 


    }*/
}

这个问题并不重复,因为我需要知道如何在不手动制作100件物品的情况下解决这个问题。

0 个答案:

没有答案