Java 使用阻塞队列传递变量

Java 使用阻塞队列传递变量,java,blockingqueue,Java,Blockingqueue,目前,我正试图让我的程序将变量xPos和yPos从ChessBoard类传递到HumanPlayer中的makeMove()方法 但目前它只在HumanPlayer中提供空指针异常 这可以通过阻塞队列来实现吗?如果可以,我做错了什么 HumanPlayer import java.util.*; import java.util.concurrent.BlockingQueue; public class HumanPlayer extends Player implements Runnab

目前,我正试图让我的程序将变量
xPos
yPos
ChessBoard
类传递到
HumanPlayer
中的
makeMove()
方法

但目前它只在
HumanPlayer
中提供空指针异常

这可以通过阻塞队列来实现吗?如果可以,我做错了什么

HumanPlayer

import java.util.*;
import java.util.concurrent.BlockingQueue;

public class HumanPlayer extends Player implements Runnable{
    private Scanner in;

    protected BlockingQueue<Integer> queue = null;
    int x;
    int y;


    public HumanPlayer(String n, Pieces p, Board b, Player o, BlockingQueue<Integer> queue) {
        super(n, p, b, o);
        this.queue = queue;
    }


    @Override
    public void run() {
        try{
             x =  queue.take();
             y =  queue.take();
        }
        catch (InterruptedException e){
            e.printStackTrace();
        }

    }

    public boolean makeMove() {
        // declare board and selected piece
        Board chess = getBoard();
        Piece selected;


        //take input of piece player wants to move
//      in = new Scanner(System.in);
//      System.out.println("Select Piece: "); 
//      System.out.print("x: ");
//      String inputX = in.nextLine().toUpperCase();
//      System.out.print("y: ");
//      String inputY = in.nextLine();
//      System.out.println(inputX+inputY);

//      //if one of the inputs is >1 length then return false to stop error
//      if (inputY.length()!=1 || inputX.length() !=1)
//          return false;

//      //change inputs to numbers to use in array
//      int x = (int)(inputX.charAt(0)-'A');
//      int y = (int)(inputY.charAt(0)-'0')-1;

        run();

        // if the input is in range of the board array select that piece if not return false
        if ( x<=7 && x>=0 && y<=7 && y>=0){             
            selected = chess.getPiece(x,y);
            System.out.println(x+""+y);
        }
        else
            return false;

        // check the piece selected is not null and is the players own piece
        if ((selected != null) 
                && (getPieces().getPiece(0).getColour() == selected.getColour() )) {
        }
        else 
            return false;

        // get the position the player wants to move the piece to
//      System.out.println("Select Move: ");
//      System.out.print("x: ");
//      String moveX = in.nextLine().toUpperCase();
//      System.out.print("y: ");
//      String moveY = in.nextLine();
//      System.out.println(moveX+moveY);

        // check the inputs length are > 1 to stop errors
//      if (moveY.length()!=1 || moveX.length() !=1)
//          return false;
//      //change inputs to ints for array
//      int mX = (int)(moveX.charAt(0)-'A');
//      int mY = (int)(moveY.charAt(0)-'0')-1;
        run();
        int mX = x;
        int mY = y;
        //check the inputs are not > 7 or less than 0
        if ( mX>7 || mX<0 || mY>7 || mY<0) {                
            return false;
        }
        //declare boolean and move
        boolean occupied = getBoard().occupied(mX, mY);
        Move requested = new Move(selected,x,y,mX,mY,occupied);

        //check if the move is and available move
        if ( selected.availableMoves().contains(requested) ){
            //delete the opponents piece if its taken
            if (occupied){
                Piece remove = chess.getPiece(mX,mY);
                chess.remove(mX, mY);
                getOpponent().getPieces().delete(remove);
            }
            // move the piece to desired position
            selected.setPosition(mX, mY);
            chess.setPosition(mX, mY, selected);
            chess.remove(x, y);         
            return true;
        }
        //return false if not and available move
        return false;
    }

}
import java.util.*;
导入java.util.concurrent.BlockingQueue;
公共类HumanPlayer扩展播放器实现Runnable{
私人扫描仪;
受保护的BlockingQueue=null;
int x;
int-y;
公共人形播放器(字符串n,片段p,棋盘b,播放器o,阻塞队列){
super(n,p,b,o);
this.queue=队列;
}
@凌驾
公开募捐{
试一试{
x=queue.take();
y=queue.take();
}
捕捉(中断异常e){
e、 printStackTrace();
}
}
公共布尔makeMove(){
//申报板和所选件
棋盘棋=getBoard();
选件;
//输入棋手想要移动的棋子
//in=新扫描仪(System.in);
//System.out.println(“选择块:”);
//系统输出打印(“x:”);
//字符串inputX=in.nextLine().toUpperCase();
//系统输出打印(“y:”);
//字符串inputY=in.nextLine();
//System.out.println(inputX+inputY);
////如果其中一个输入的长度大于1,则返回false以停止错误
//if(inputY.length()!=1 | | inputX.length()!=1)
//返回false;
////将输入更改为要在数组中使用的数字
//intx=(int)(inputX.charAt(0)-'A');
//int y=(int)(inputY.charAt(0)-'0')-1;
run();
//如果输入在线路板阵列的范围内,如果不返回false,则选择该块
如果(x=0&&y=0){
选定=国际象棋.getPiece(x,y);
System.out.println(x+“”+y);
}
其他的
返回false;
//检查所选棋子是否为空,是否为玩家自己的棋子
如果((选定!=null)
&&(getPieces().getPiece(0.getColor()==选定的.getColor())){
}
其他的
返回false;
//获得玩家想要移动棋子的位置
//System.out.println(“选择移动:”);
//系统输出打印(“x:”);
//字符串moveX=in.nextLine().toUpperCase();
//系统输出打印(“y:”);
//字符串moveY=in.nextLine();
//System.out.println(moveX+moveY);
//检查输入长度是否大于1以停止错误
//如果(moveY.length()!=1 | | moveX.length()!=1)
//返回false;
////将数组的输入更改为int
//intmx=(int)(moveX.charAt(0)-'A');
//int mY=(int)(moveY.charAt(0)-'0')-1;
run();
int mX=x;
int mY=y;
//检查输入是否大于7或小于0
如果(mX>7 | | mX7 | | mY=0;y--){
对于(int x=0;x=0;y--){

对于(int x=0;x您能指出错误的确切位置吗?您打印了堆栈跟踪吗?顺便说一句,在棋盘构造器中,您从未实例化队列。它被设置为null,然后您尝试将传递的队列作为参数分配给它。@EvanBechtol null错误是当我试图在HumanPlayer中从队列中获取项目时发生的
import java.awt.*;
import java.awt.event.*;
import java.util.concurrent.BlockingQueue;

import javax.swing.*;

@SuppressWarnings("serial")
public class ChessBoard extends JFrame implements ActionListener, Runnable{
    JButton[][] squares;
    private int xPos;
    private int yPos;
    protected BlockingQueue<Integer> queue = null;
    public ChessBoard(BlockingQueue<Integer> queue){
        this.queue = queue;
        setSize(800,800);
        setTitle("chess");
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container contentPane = getContentPane();
        contentPane.setLayout(new GridLayout (8,8));
        squares = new JButton[8][8];
        Color lightBrown = new Color(222,184,135);
        Color Brown = new Color(167,71,20);
        for(int y=7; y>=0; y--){
            for(int x=0; x<8; x++){
                squares[x][y] = new JButton();
                squares[x][y].addActionListener(this);

                if ((x + y) % 2 == 0) {
                    squares[x][y].setBackground(Brown);
                } 
                else {
                    squares[x][y].setBackground(lightBrown);
                }  
                contentPane.add(squares[x][y]);
            }
        }
        setVisible(true);
    }

    public JButton getButton(int x, int y){
        return squares[x][y];           
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton selected = (JButton) e.getSource();
        for(int y=7; y>=0; y--){
            for(int x=0; x<8; x++){
                if( selected == squares[x][y]){
                    xPos = x;
                    yPos = y;   
                    System.out.println(xPos+""+yPos);
                }
            }
        }
    }

    public int getxPos() {
        return xPos;
    }


    public int getyPos() {
        return yPos;
    }

    @Override
    public void run() {
        try{
            queue.put(xPos);
            Thread.sleep(1000);
            queue.put(yPos);
        }
        catch (InterruptedException e){
            e.printStackTrace();

        }
    }



}