Ich schreibe momentan an einem kleinen Toolprogramm um Java zu üben und mal wieder ein Problem:
Folgendes Problem bei folgendem Sourcecode:
Beim Ausführen bekomme ich folgende Fehlermeldung:
Exception in thread "main" java.lang.ExceptionInInitializerError
at main.Main.starter(Main.java:57)
at main.Main.main(Main.java:41)
Caused by: java.lang.NullPointerException
at main.Utils.<clinit>(Utils.java:15)
... 2 more
Ich weiß nicht, was ein ExceptionInInitializerError ist, vielleicht kann mir das ja jemand erklären, im Idealfall sogar ohne Link zu einer englischsprachigen Seite, sondern in verständlichem Deutsch. Im Idealfall sogar mit problemlösung. Danke im Vorraus
Folgendes Problem bei folgendem Sourcecode:
Java:
package main;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import javax.swing.*;
import algebra.*;
import geometry.*;
import strings.*;
import java.awt.Color;
/**
* The class which starts JTool.
* @author Mario Trageser
*
*/
public class Main
{
private static boolean stop = false;
private static final UserInterface userInterface = new UserInterface();
public static boolean isStop()
{
return stop;
}
public static void setStop(boolean stop)
{
Main.stop = stop;
}
public static UserInterface getUserInterface()
{
return userInterface;
}
/**
* Starts the program.
* @param args
* @throws FileNotFoundException
* @throws UnsupportedEncodingException
*/
public static void main(String[] args) throws FileNotFoundException,
UnsupportedEncodingException
{
while(!(stop))
{
starter();
}
System.exit(0);
}
/**
* Creates the main-class.
*/
/**
* Opens a window where a tool can be selected.
* @throws FileNotFoundException
* @throws UnsupportedEncodingException
*/
private static void starter() throws FileNotFoundException,
UnsupportedEncodingException
{
final String[] toolTypes = {"algebra", "geometry", "strings", "exit"};
final int toolTypeNumber = Utils.selectTool(
"Select the tool's type you want to use!", "select tool type",
toolTypes);
userInterface.addText(toolTypes[toolTypeNumber]);
if(toolTypeNumber == 0)
{
final String[] toolList = {"calculator", "primality test",
"Collatz Sequence", "averager", "day of year",
"Pascals Trianglesorter"};
final int toolNumber = Utils.selectTool(
"Select the tool you want to use!", "tool selection",
toolList);
userInterface.addText("-->" + toolList[toolNumber]);
switch (toolNumber)
{
case 0:
new Calculator();
break;
case 1:
new PrimalityTest();
break;
case 2:
new CollatzSequence();
break;
case 3:
new Averager();
break;
case 4:
new DayOfYear();
break;
case 5:
new PascalsTriangle();
break;
case 6:
new Sorter();
break;
}
}
if(toolTypeNumber == 1)
{
final String[] toolList = {"point distance calculator",
"polygon containing" +
" test"};
final int toolNumber = Utils.selectTool(
"Select the tool you want to use!", "tool selection",
toolList);
userInterface.addText("-->" + toolList[toolNumber]);
switch (toolNumber)
{
case 0:
new PointDistanceCalculator();
break;
case 1:
new PolygonContainingTest();
break;
}
}
if(toolTypeNumber == 2)
{
final String[] toolList = {"anagram test", "String searcher",
"String splitter", "String encoder"};
final int toolNumber = Utils.selectTool(
"Select the tool you want to use!", "tool selection",
toolList);
userInterface.addText("-->" + toolList[toolNumber]);
switch (toolNumber)
{
case 0:
new AnagramTest();
break;
case 1:
new StringSearcher();
break;
case 2:
new StringSplitter();
break;
case 3:
new StringEncoder();
break;
}
}
if(toolTypeNumber == 3)
{
stop = Utils.yesNo("Do you want to exit JTool?", "exit");
}
}
}
Java:
package main;
import javax.swing.*;
import java.awt.Color;
/**
* The graphical user interface on which is shown a text.
* @author Mario Trageser
*
*/
public class UserInterface
{
private final JLabel l;
private final JFrame f;
public JLabel getL()
{
return l;
}
public JFrame getF()
{
return f;
}
/**
* Creates an empty UserInterface.
*/
public UserInterface()
{
f = new JFrame("JTool");
l = new JLabel();
f.setBounds(0, 0, 275, 125);
f.add(l);
f.setVisible(true);
}
/**
* Sets the text.
* @param s The text.
*/
public void setText(String s)
{
l.setText(s);
}
/**
* Adds a text to the current text.
* @param s The text to be added.
*/
public void addText(String s)
{
setText(l.getText() + s);
}
/**
* Clears the text.
*/
public void clearText()
{
l.setText("");
}
/**
* Changes the text's color.
* @param c The text's color.
*/
public void setTextColor(Color c)
{
l.setForeground(c);
}
}
Java:
package main;
import java.awt.*;
import java.util.regex.Pattern;
import javax.swing.*;
import java.io.*;
import java.util.Scanner;
/**
* A class that contains methods which are used by several classes.
* @author Mario Trageser
*
*/
public class Utils
{
private static UserInterface userInterface;
private static JFrame f = userInterface.getF();
public JFrame getF()
{
return f;
}
public static void setUserInterface(UserInterface userInterface)
{
Utils.userInterface = userInterface;
}
public UserInterface getUserInterface()
{
return userInterface;
}
/**
* Returns an integer variable by opening a window where a number has to be
* entered.
* If the entered String is no number, you have to enter the number again.
* @param f The JFrame where the JOptionPane will be shown on.
* @param message The message which will be shown when you have to enter an
* number.
* @param title The title of the window where you have to enter a number.
* @return int
*/
public static int enterInt(String message, String title)
{
String s = JOptionPane.showInputDialog(f, message, title, 3).trim();
boolean isInt = Pattern.matches("[1234567890]*", s);
while (!(isInt))
{
if(!(isInt))
{
errorMessage(s + " is no number.", "not a number");
}
s = JOptionPane.showInputDialog(f, message, title, 3);
isInt = Pattern.matches("[0-9]*", s);
}
final int i = Integer.parseInt(s);
return i;
}
/**
* Creates an error message on a window.
* @param f The JFrame where the JOptionPane will be shown on.
* @param message The error message you will receive.
* @param title The error message's title.
*/
public static void errorMessage(String message, String title)
{
JOptionPane.showMessageDialog(f, "error", title, 0 );
userInterface.setText(message);
userInterface.setTextColor(Color.RED);
}
/**
* Creates a numbered Point with x and y coordinates which have to be
* entered.
* @param f The JFrame where the window where you have to enter the Point's
* coordinates will be shown on.
* @param continuous The Point's number.
* @return java.awt.Point
*/
public static Point newPoint(int continuous)
{
final int xCoordinate = Utils.enterInt("Enter the point" +
continuous + "'s x-coordinate!", "x-coordinate");
if(xCoordinate < 0)
{
Utils.errorMessage("The coordinates have to be positiv.",
"coordinate negativ");
newPoint(continuous);
}
final int yCoordinate = Utils.enterInt("Enter the point" +
continuous + "'s y-coordinate!", "y-coordinate");
if(yCoordinate < 0)
{
Utils.errorMessage("The coordinates have to be positiv.",
"coordinate negativ");
newPoint(continuous);
}
final Point p = new Point(xCoordinate, yCoordinate);
return p;
}
/**
* Creates a Point with x and y coordinates which have to be entered.
* @param f The JFrame where the window where you have to enter the Point's
* coordinates will be shown on.
* @return java.awt.Point
*/
public static Point newPoint()
{
final int xCoordinate = Utils.enterInt(
"Enter the point's x-coordinate!", "x-coordinate");
if(xCoordinate < 0)
{
Utils.errorMessage("The coordinates have to be positiv.",
"coordinate negativ");
newPoint();
}
final int yCoordinate = Utils.enterInt(
"Enter the point's y-coordinate!", "y-coordinate");
if(yCoordinate < 0)
{
Utils.errorMessage("The coordinates have to be positiv.",
"coordinate negativ");
newPoint();
}
final Point p = new Point(xCoordinate, yCoordinate);
return p;
}
/**
* Creates a window where you have to select a tool.
* @param f The JFrame where the JOptionPane where you have to select the
* tool will be shown on.
* @param text The text which is shown when you have to select a tool.
* @param title The title which is shown on the window where you have to
* select the tool.
* @param options The options for the tool selection.
* @return
*/
public static int selectTool(String text, String title, String[] options)
{
final int toolNumber = JOptionPane.showOptionDialog(f, text, title, 0, 3, null, options, null);
return toolNumber;
}
/**
* Creates a window where yes or no can be selected.
* @param f The JFrame where the window is shown on.
* @param text The text which is shown on the window.
* @param title The window's title.
* @return int
*/
public static boolean yesNo(String text, String title)
{
boolean b = false;
String[] yesNo = {"yes", "no"};
final int i = selectTool(text, title, yesNo);
if(i == 0)
b = true;
return b;
}
/**
* Opens a window where you have to enter a String.
* @param message
* @return
*/
public static String enterString(JFrame f, String message)
{
final String s = JOptionPane.showInputDialog(f, message, "enter string",
3);
return s;
}
/**
* Opens a window where a regular expression has to be selected.
* @return String
*/
public static String selectRegularExpression()
{
String regularExpression = "";
final String[] regularExpressions = {"character", "number",
"whitespace", "letter", "blank/tab", "lowercase letter",
"uppercase letter", "alphanumeric character", "point character",
"printable letter"};
final String[] includes = {"yes","no"};
final int kindOfExpression = Utils.selectTool(
"Select the kind of regular expression you want to search?",
"select regular expression", regularExpressions);
switch(kindOfExpression)
{
case 0:
regularExpression = "\\.";
break;
case 1:
regularExpression = "\\d";
break;
case 2:
regularExpression = "\\s";
break;
case 3:
regularExpression = "\\w";
break;
case 4:
regularExpression = "\\p{Blank}";
break;
case 5:
regularExpression = "\\p{Lower}";
break;
case 6:
regularExpression = "\\p{Upper}";
break;
case 7:
regularExpression = "\\p{Alnum}";
break;
case 8:
regularExpression = "\\p{Punct}";
break;
case 9:
regularExpression = "\\p{Print}";
break;
}
final int included = Utils.selectTool(
"Should the regular expression be included or not?",
"included?", includes);
if(included == 1)
{
switch (kindOfExpression)
{
case 0:
regularExpression = "^\\.";
break;
case 1:
regularExpression = "^\\d";
break;
case 2:
regularExpression = "^\\s";
break;
case 3:
regularExpression = "^\\w";
break;
case 4:
regularExpression = "^\\p{Blank}";
break;
case 5:
regularExpression = "^\\p{Lower}";
break;
case 6:
regularExpression = "^\\p{Upper}";
break;
case 7:
regularExpression = "^\\p{Alnum}";
break;
case 8:
regularExpression = "^\\p{Punct}";
break;
case 9:
regularExpression = "\\p{Print}";
break;
}
}
return regularExpression;
}
/**
* Returns an entered String or a by a path selected to String converted
* text-file.
* @param f The JFrame where the String or the path have to be entered.
* @param message The message that is shown when the String or the path have
* to be entered.
* @return String
* @throws FileNotFoundException
*/
public static String enterFileOrString(String message)
throws FileNotFoundException
{
final String[] stringOptions = {"entered String", ".txt-file"};
String s = "";
final int i = Utils.selectTool(
"Which kind of string do you want to use?",
"select kind of string", stringOptions);
if(i == 0)
s = enterString(f, message);
else
{
message = message.replace("string", ".txt-file's path");
final String path = JOptionPane.showInputDialog(f, message,
"enter path", 4).replace("\\", "/");
final Scanner scanner = new Scanner(new File(path));
while(scanner.hasNextLine())
s += scanner.nextLine();
System.out.println(s);
}
return s;
}
}
Exception in thread "main" java.lang.ExceptionInInitializerError
at main.Main.starter(Main.java:57)
at main.Main.main(Main.java:41)
Caused by: java.lang.NullPointerException
at main.Utils.<clinit>(Utils.java:15)
... 2 more
Ich weiß nicht, was ein ExceptionInInitializerError ist, vielleicht kann mir das ja jemand erklären, im Idealfall sogar ohne Link zu einer englischsprachigen Seite, sondern in verständlichem Deutsch. Im Idealfall sogar mit problemlösung. Danke im Vorraus