程序无法向 JTextArea 添加文本

问题描述 投票:0回答:1

我编写了一个程序,它创建一个名为 Window() 的 GUI,并指示我的 Finch 机器人跟随一个物体。当我运行该程序时,没有文本添加到 JTextArea。它应该显示“请点击 Finch 来激活”。

我的程序告诉我这一行是错误的根源:

NewOption5.feed.append("Please Tap Finch to Activate!");

控制台:

正在连接到 Finch...这可能需要几秒钟...

线程“main”中的异常 java.lang.NullPointerException

在 NewOption5.ProgramFollow(NewOption5.java:58)

在 NewOption5.main(NewOption5.java:12)

代码:

import edu.cmu.ri.createlab.terk.robot.finch.Finch;
import java.awt.*;
import javax.swing.*;

 public class NewOption5 {

    static JTextArea feed;

    static Finch myFinch = new Finch();

public static void main(String[] args) {
    //Calling Window    
//  myFinch = new Finch();
    Window();
    ProgramFollow();



}



// Method for creating a GUI

//static Finch myFinch = new Finch();
public static void Window()
    {
  // Create the window
    JFrame x = new JFrame("Finch Mission : Follow an Object!");

 // How the window should be closed
    x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//  Adding a layout manager
    x.setLayout(new FlowLayout());

//  Adding components ( significantly a JTextArea being a feedback box )
    x.add(new JButton("Halt!"));
    x.add(new JButton("Exit"));
    final JTextArea feed = new JTextArea(30,50);
    JScrollPane feedscroll = new JScrollPane(feed);
    x.add(feedscroll);


// Arrange components neatly inside the window
    x.pack();

// Making the window visible once Window is called
    x.setVisible(true);
//  ProgramFollow();
}





public static void ProgramFollow() {

    // Loop to wait for Finch to be tapped and an obstacle detected in front of the Finch   
    boolean StartProgram;
    NewOption5.feed.append("Please Tap Finch to Activate!");    

    while (StartProgram = true)
    {   
    // Sending message to the feedback box
    feed.append("Please Tap Finch to Activate!");   

    // Conditional statement for same thing as mentioned above  

    if (myFinch.isTapped()==true && myFinch.isObstacleLeftSide()==true && myFinch.isObstacleRightSide()==true)
    {   
    NewOption5.feed.append("Finch is activated! Object is detected");   
    myFinch.setLED(red,0,0);
    myFinch.setWheelVelocities(leftVelocity, rightVelocity);

    // Triggers RunAgain to true so the program doesnt stop in one run in order for Finch to move continuosuly


    boolean RunAgain=true;




    while(RunAgain)
    {

    // Calling Movement method  for Finch movements

    Movement();

    // Inside while(RunAgain loop , there is a conditional statment which makes the Finch terminate the program after tapping it twice

    if (myFinch.isTapped()==true && myFinch.isTapped()==true)
    {



    }
    }
    }
    }
    }



    // Method for Finch movements

    public static  void Movement()

    {

    if (myFinch.isObstacleLeftSide()==false && myFinch.isObstacleRightSide()==false)

    {

    // send message to the feedback box ("Object is Detected! Following it now!");
    NewOption5.feed.append("Following the Object now!");

    StraightMovement();

    }

    else if (myFinch.isObstacleLeftSide()==true && myFinch.isObstacleRightSide()==false)

    {

    NewOption5.feed.append("Object detected on the left side"); 

    LeftMovement();

    }

    else if (myFinch.isObstacleLeftSide()==false && myFinch.isObstacleRightSide()==true)

    {

    NewOption5.feed.append("Object detected on the right side");

    RightMovement();

    }

    else if (myFinch.isObstacleLeftSide()==true && myFinch.isObstacleRightSide()==true)

    {

    NewOption5.feed.append("Stopped now");  

    StopMovement();

    }

    }


    // Area of variables declaration for easy value modifications

    static int Buzz = 340;
    static int BuzzDuration = 10;


    static int red = 255;
    static int green = 255;

    static int leftVelocity = 100;
    static int rightVelocity = 100;

    static int leftTurnV = -50;
    static int rightTurnV = -50;;


    // Area of variables declaration for easy value modifications


    public static void StraightMovement()

    {

    myFinch.setLED(0, green, 0);

    myFinch.setWheelVelocities(leftVelocity, rightVelocity);

    myFinch.buzz(Buzz, BuzzDuration);

    }

    public static void LeftMovement()

    {

    myFinch.setLED(0, green, 0);

    myFinch.setWheelVelocities(leftTurnV, rightVelocity);

    myFinch.buzz(Buzz, BuzzDuration);

    }

    public static void RightMovement()

    {

    myFinch.setLED(0, green, 0);

    myFinch.setWheelVelocities(leftVelocity, rightTurnV);

    myFinch.buzz(Buzz, BuzzDuration);

    }

    public static void StopMovement()

    {

    myFinch.setLED(red, 0 , 0);

    myFinch.stopWheels();

    myFinch.buzz(Buzz, BuzzDuration);

    }


    }
java eclipse swing finch
1个回答
0
投票

您没有正确初始化

feed

final JTextArea feed = new JTextArea(30,50);
这不会初始化字段
feed
,而是创建一个同名的新局部变量

尝试将

final JTextArea feed = new JTextArea(30,50);
变成
feed = new JTextArea(30,50);

© www.soinside.com 2019 - 2024. All rights reserved.