-1

Window() という GUI を作成し、Finch ロボットにオブジェクトを追跡するように指示するプログラムを作成しました。プログラムを実行すると、JTextArea にテキストが追加されません。「フィンチをタップしてアクティブ化してください」と表示されます。

私のプログラムは、この行がエラーの原因であることを教えてくれます:

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

コンソール :

Finch に接続しています...これには数秒かかる場合があります...

スレッド「メイン」での例外 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);

    }


    }
4

1 に答える 1

0

feed正しく初期化されていません。

final JTextArea feed = new JTextArea(30,50); これはフィールドを初期化せずfeed、代わりに同じ名前の新しいローカル変数を作成します。

final JTextArea feed = new JTextArea(30,50);に変えてみてくださいfeed = new JTextArea(30,50);

于 2015-01-30T11:08:19.460 に答える