1 year ago
#243711
Alwin Binu
Error when trying to read in from a text file. including InvocationTargetException, .RuntimeException and NumberFormatException
Hi to print out values from values from a file but I am getting this error. Can anyone help point out the problem. I am using a pre made class for the exact format of the data from the file using toString. Therefore the problem is with the code I just provided and I am not sure where I have gone wrong.
Please enter a shape file to open: TwoRedCircles.txt *
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:467)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:366)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:903)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NumberFormatException: For input string: "true"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:668)
at java.base/java.lang.Integer.parseInt(Integer.java:786)
at com.example.demo/com.example.demo.ReadShapeFile.createCircle(ReadShapeFile.java:32)
at com.example.demo/com.example.demo.ReadShapeFile.readDataFile(ReadShapeFile.java:60)
at com.example.demo/com.example.demo.ReadShapeFile.readDataFile(ReadShapeFile.java:82)
at com.example.demo/com.example.demo.BouncingShapesWindow.initShapes(BouncingShapesWindow.java:56)
at com.example.demo/com.example.demo.BouncingShapesWindow.<init>(BouncingShapesWindow.java:42)
at com.example.demo/com.example.demo.Main.start(Main.java:43)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:849)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:474)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:447)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:446)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
Exception running application com.example.demo.Main
This is my code below.
package com.example.demo;
import javafx.scene.paint.Color; import java.io.*; import java.util.Scanner;
public class ReadShapeFile {
private static Circle createCircle(String[] currentShape) {
// <px> <py> <vx> <vy> <filled?> <diameter><r> <g> <b> <insertion time>
int px = Integer.parseInt(currentShape[1]);
int py = Integer.parseInt(currentShape[2]);
int vx = Integer.parseInt(currentShape[3]);
int vy = Integer.parseInt(currentShape[4]);
boolean filled = false;
if (currentShape[5].equals("true")) {
filled = true;
}
int diameter = Integer.parseInt(currentShape[6]);
int r = Integer.parseInt(currentShape[7]);
int g = Integer.parseInt(currentShape[8]);
int b = Integer.parseInt(currentShape[9]);
Color color = Color.rgb(r, g, b);
int insertionTime = Integer.parseInt(currentShape[10]);
return new Circle(insertionTime, px, py, vx, vy, diameter, color, filled);
}
/**
* Reads the data file used by the program and returns the constructed queue
*
* @param in the scanner of the file
* @return the queue represented by the data file
*/
private static Queue<ClosedShape> readDataFile(Scanner in) {
Queue<ClosedShape> shapeQueue = new Queue<ClosedShape>();
//read in the shape files and place them on the Queue
//Right now, returning an empty Queue. You need to change this.
while (in.hasNextLine()) {
String[] currentShape = in.nextLine().split(" ");
if (currentShape[0].equals("circle")) {
ClosedShape shape = createCircle(currentShape);
shapeQueue.enqueue(shape);
System.out.println(shape.toString());
}
}
return shapeQueue;
}
/**
* Method to read the file and return a queue of shapes from this file. The
* program should handle the file not found exception here and shut down the
* program gracefully.
*
* @param filename the name of the file
* @return the queue of shapes from the file
*/
public static Queue<ClosedShape> readDataFile(String filename) {
// HINT: You might want to open a file here.
File newFile = new File(filename);
try {
Scanner in = new Scanner(newFile); //null is not sensible. Please change
return ReadShapeFile.readDataFile(in);
} catch (FileNotFoundException e) {
System.out.print("Could not find " + filename);
System.exit(0);
}
return null;
}
}
java
runtimeexception
numberformatexception
invocationtargetexception
0 Answers
Your Answer