2017-10-09 17 views
1

현재 학교 행맨 게임을 프로그래밍 중입니다.HangMan JTexfield ActionListener java.lang.NullPointerException

는이 오류 어떻게해야합니까 : "AWT-EventQueue의-0"java.lang.NullPointerException이에게 스레드에서 예외

이 내 클래스 HangManBackend입니다 :

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.Scanner; 
import java.util.concurrent.TimeUnit; 

/** 
* Created by Jhidzzo-Admin on 07.10.2017. 
* Project: Hangman 
*/ 

class HangManBackend 
{ 
    static String words[] = new String[10]; 
    static String inputSTR; 
    static boolean userInput; 
    static char input; 
    static char inputChar; 
    static Scanner reader = new Scanner(System.in); 
    static boolean sucess = false; 
    static boolean fail = false; 
    static int found = 0; 
    static int guessedChars = 0; 
    static int fails = 0; 
    static int rngI; 
    static char unkownChars[]; 
    static char knownChars[]; 

    static void backend(int rngI) 
    { 

     words[0] = "Banane"; 
     words[1] = "Apfel"; 
     words[2] = "Birne"; 
     words[3] = "Blau"; 
     words[4] = "Grün"; 
     words[5] = "Gelb"; 
     words[6] = "Rot"; 
     words[7] = "Weiß"; 
     words[8] = "Orange"; 
     words[9] = "Grau"; 

     int length = words[rngI].length(); 
     char unkownChars[] = words[rngI].toCharArray(); 
     char knownChars[] = new char[length]; 

     for (int j = 0; j < knownChars.length; j++) 
     { 
      knownChars[j] = '-'; 
     } 

     System.out.println(unkownChars); 

     System.out.println("Guess a letter!"); 
    }//End method 

    static void checkUserInput() 
    { 
     while (!sucess && !fail) 
     { 
      if (fails < 3) 
      { 
       //char input = reader.next(".").charAt(0); 
       System.out.println(inputChar + " Input given!"); 
       input = inputChar;       // Here happens the error 
       for (int i = 0; i < unkownChars.length; i++) 
       { 
        if (input == unkownChars[i] || input == Character.toLowerCase(unkownChars[i])) 
        { 
         knownChars[i] = unkownChars[i]; 
         found++; 
        } 
       } 
       if (found == 0) 
       { 
        fails++; 
        System.out.println("Nope letter is not in there!"); 
        if (fails < 3) 
        { 
         System.out.println("Enter your next guess!"); 
        } 
       } else 
       { 
        guessedChars = guessedChars + found; 
        if (found == 1) 
        { 
         System.out.println("Cool you found " + found + " letter!"); 
        } else if (found > 1) 
        { 
         System.out.println("Cool you found " + found + " letters!"); 
        } 
        found = 0; 
        if (guessedChars != knownChars.length) 
        { 
         System.out.println(knownChars); 
         System.out.println("Enter next guess!"); 
        } 
       } 
       if (guessedChars == knownChars.length) 
       { 
        sucess = true; 
        System.out.println("Good job you got the whole word: " + words[rngI]); 
       } 
      } else if (fails >= 3) 
      { 
       System.out.println("You failed!"); 
       fail = true; 
      } 
      userInput = false; 
     }//End while 
    } 

    static int getWordsLength() 
    { 
     int WLength = words.length; 

     return WLength; 

    } 

    static void setInput(String inputA) 
    { 
     inputSTR = inputA; 
     inputChar = inputSTR.charAt(0); 
     checkUserInput(); 
    } 

    static void listener() 
    { 
     Main.submit.addActionListener(new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent e) 
      { 
       setInput(Main.input.getText()); 
       Main.input.setText(null); 
      } 
     }); 
    } 

} 

나는 오류가 어디서 어떻게 생각 나는 그것을 표시했다. 나는 그것을 고치기위한 어떤 생각을 가지고 있지 않다. .. 나는 동기화를 사용하려고 노력했다. 그러나 나는 정말로 그것을 얻지 않는다.

답장을 보내 주셔서 감사합니다.

Btw 나는 독일 출신이므로 나쁜 영어는 신경 쓰지 않아. : D

편집 :이

import jdk.internal.util.xml.impl.Input; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

/** 
* Created by Jhidzzo-Admin on 07.10.2017. 
* Project: Hangman 
*/ 

public class Main 
{ 
    static JTextField input = new JTextField(10); 
    static JButton submit = new JButton("Try your Luck"); 
    public static void main(String[] args) 
    { 



     JFrame frame = new JFrame("HangMan"); 

     frame.add(input); 
     frame.add(submit); 

     /* 
     --------------- 
      Layout 
     --------------- 
     */ 

     frame.setLayout(new GridBagLayout()); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridwidth = GridBagConstraints.REMAINDER; 
     gbc.fill = GridBagConstraints.HORIZONTAL; 

     /* 
     --------------- 
      General 
     --------------- 
     */ 

     frame.setSize(500, 300); 
     frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     frame.setVisible(true); 

     int rng1 = randomWithRange(0, HangManBackend.getWordsLength() - 1); 

     HangManBackend.listener(); 
     HangManBackend.backend(rng1); 
    } 
    static int randomWithRange(int min, int max) 
    { 
     int range = (max - min) + 1; 
     return (int) (Math.random() * range) + min; 
    } 
} 

EDIT 홈페이지 내 클래스입니다 : 그래서

  System.out.println(inputSTR + " Input given!"); 
      if (inputSTR != null) 
      { 
       input = inputSTR.charAt(0); 
      }else 
      { 
       listener(); 
      } 

을 추가하고 여전히 작품 ...

+1

[NullPointerException은 무엇이며 어떻게 수정합니까?] (https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it/) – Grimthorr

+0

"당신이 물어 본 예외는 변수를 선언했지만 객체를 만들지 않았을 때 발생합니다." 이게 나를위한 사건인가? sry 나는 아직도 그것을 얻지 못한다 ... – Jhidzzo

+0

'Main.submit'이 선언 된 곳이 보이지 않는다. 당신의 모든 소스를 올리실 수 있습니까? – markspace

답변

1

시도는이 줄을 추가하지 백엔드 기능 시작 부분

static void backend(int rngI) 
{ 
    if(rngI > 9 || rngI<0) 
    {System.out.println("number not supported "); 
    } 
+0

작동하지 않습니다. 내 수업 Main에 rngI가 추가되었습니다. – Jhidzzo