Задать вопрос
dalv_happy
@dalv_happy

Exception in thread «AWT-EventQueue-0»?

Пишу парсер кода, но возникает ошибка вида, когда я второй раз подряд кликаю по кнопке вызова функции main
Ошибка:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at TakeTheToken.main(TakeTheToken.java:34)
at Win_Token$2.actionPerformed(Win_Token.java:57)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TakeTheToken {

	public class object {
		String token;
		int type_token;		
	}

	public String token;
	public int type_token;

	public TakeTheToken(String token, int type_token) {
		this.token = token;
		this.type_token = type_token;
	}

	static TakeTheToken ex[];
	public static int count = 0;

	public static String main() throws IOException {
		String str = Open();
		System.out.println(str);
		addToken(str);
		String s = "";
		for (int i = 0; i < count; i++) {// Вывод объекта
			s += "\n" + ex[i].token + " " + ex[i].type_token + "\n";
		}
		
		return s;
	}

	public static String Open() throws IOException {
		File f = new File("D:\\1.txt");
		FileReader fr = null;
		try {
			fr = new FileReader(f);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		@SuppressWarnings("resource")
		BufferedReader br = new BufferedReader(fr);
		StringBuffer sb = new StringBuffer();
		String eachLine = null;
		try {
			eachLine = br.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		while (eachLine != null) {
			sb.append(eachLine);
			sb.append("\n");
			try {
				eachLine = br.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}



		return sb.toString().replaceAll("[\\t]", " ");

	}


	public static void addToken(String str) { //Разбивает на токены
		int c = 0;
		ex = new TakeTheToken[str.length()];
		char[] type = str.toCharArray();
		int i = 0;
		String word = "", number = "", charius = "";


		while (i < str.length()) {

			String typeChar = Character.toString(type[i]);
			if (RegExp(typeChar) == 1) {//Если буквы        

				while (RegExp(typeChar) == 1) {                 
					word += typeChar;                   
					i++;
					if (i < str.length()) {
						typeChar = String.valueOf(type[i]);
					} else break;               
				}


				ex[c] = new TakeTheToken(word, typeToken(word, 1));
				c++;
				count++;
				System.out.print(word);
				word = "";

			} 

			if (RegExp(typeChar) == 2) {//Если цифры
				while (RegExp(typeChar) == 2) {                 
					number += typeChar;                 
					i++;
					if (i < str.length()) {
						typeChar = String.valueOf(type[i]);
					} else break;
				}

				ex[c] = new TakeTheToken(number, 3);
				c++;
				count++;

				System.out.print(number);
				number = "";

			} 

			if (RegExp(typeChar) == 3) { 
				if (type[i] == '+') {
					while (type[i] == '+') {
						charius += typeChar;						
						if (i+1 < str.length() && type[i+1] == '+') {
							i++;
							typeChar = String.valueOf(type[i]);
						} else break;
					}
					ex[c] = new TakeTheToken(charius, 4);
					c++;
					count++;
					System.out.print(charius);
					charius = "";
				}


				if (type[i] == '-') {
					while (type[i] == '-') {
						charius += typeChar;						
						if (i+1 < str.length() && type[i+1] == '-') {
							i++;
							typeChar = String.valueOf(type[i]);
						} else break;
					}
					ex[c] = new TakeTheToken(charius, 4);
					c++;
					count++;
					System.out.print(charius);
					charius = "";
				}


				if (type[i] == '"') {
					charius += typeChar;
					ex[c] = new TakeTheToken(charius, 5);
					c++;
					count++;
					System.out.print(charius);
					charius = "";
					i++;
					typeChar = String.valueOf(type[i]);
					while (type[i] != '"') {											
						if (i+1 < str.length() && type[i] != '"') {							
							typeChar = String.valueOf(type[i]);
							i++;
							charius += typeChar;	
						}
					}
					ex[c] = new TakeTheToken(charius, 3);
					c++;
					count++;
					System.out.print(charius);
					charius = "";


					typeChar = String.valueOf(type[i]);
					charius += typeChar;
					ex[c] = new TakeTheToken(charius, 5);
					c++;
					count++;
					System.out.print(charius);
					charius = "";	
				}	

				if (type[i] == '\'') {
					charius += typeChar;
					ex[c] = new TakeTheToken(charius, 5);
					c++;
					count++;
					System.out.print(charius);
					charius = "";
					i++;
					typeChar = String.valueOf(type[i]);
					while (type[i] != '\'') {											
						if (i+1 < str.length() && type[i] != '\'') {							
							typeChar = String.valueOf(type[i]);
							i++;
							charius += typeChar;	
						}
					}
					ex[c] = new TakeTheToken(charius, 3);
					c++;
					count++;
					System.out.print(charius);
					charius = "";


					typeChar = String.valueOf(type[i]);
					charius += typeChar;
					ex[c] = new TakeTheToken(charius, 5);
					c++;
					count++;
					System.out.print(charius);
					charius = "";	
				}	

				if (type[i] == '!') {	
					typeChar = String.valueOf(type[i]);
					charius += typeChar;
					if (type[i+1] == '=') {	
						i++;
						typeChar = String.valueOf(type[i]);						
						charius += typeChar;	
						ex[c] = new TakeTheToken(charius, 4);
						c++;
						count++;
						System.out.print(charius);
						charius = "";
					} else {
						ex[c] = new TakeTheToken(charius, 4);
						c++;
						count++;
						System.out.print(charius);
						charius = "";
					}		
				}

				if (type[i] == '=') {	
					typeChar = String.valueOf(type[i]);
					charius += typeChar;
					if (type[i+1] == '=') {	
						i++;
						typeChar = String.valueOf(type[i]);						
						charius += typeChar;	
						ex[c] = new TakeTheToken(charius, 4);
						c++;
						count++;
						System.out.print(charius);
						charius = "";
					} else {
						ex[c] = new TakeTheToken(charius, 4);
						c++;
						count++;
						System.out.print(charius);
						charius = "";
					}		
				}

				if (type[i] == '<') {	
					typeChar = String.valueOf(type[i]);
					charius += typeChar;
					if (type[i+1] == '=') {	
						i++;
						typeChar = String.valueOf(type[i]);						
						charius += typeChar;	
						ex[c] = new TakeTheToken(charius, 4);
						c++;
						count++;
						System.out.print(charius);
						charius = "";
					} else {
						ex[c] = new TakeTheToken(charius, 4);
						c++;
						count++;
						System.out.print(charius);
						charius = "";
					}		
				}

						
				}
				
			}	

			i++;
		}   

	}

}
  • Вопрос задан
  • 177 просмотров
Подписаться 1 Оценить Комментировать
Пригласить эксперта
Ответы на вопрос 1
dalv_happy
@dalv_happy Автор вопроса
Вот так вот.
public static String main(String[] args) throws IOException {
		String str = Open();
		System.out.println(str);
		addToken(str);
		String s = "";
		for (int i = 0; i < count; i++) {// Вывод объекта	
			if (ex[i] != null) {
				s += ex[i].token + " " + ex[i].type_token + "\n";
			}
		}
		ex = new TakeTheToken[0];
		count = 0;
		return s;
	}
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы