Output Student data to the Console
I have made the Student data into a List of Student objects, I want to output the contents of the List to the Console. I don't understand what this error means. Any clarification is much appreciated!
Exception in thread "main" java.lang.RuntimeException: java.io.FileNotFoundException: scr\main\webapp\assets\JavaWebProgramming.xlsx
at StudentMain.main(StudentMain.java:23)
Caused by: java.io.FileNotFoundException: scr\main\webapp\assets\JavaWebProgramming.xlsx
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:293)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:271)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:252)
at utility.WorkbookUtility.retrievePeopleFromWorkbook(WorkbookUtility.java:17)
at StudentMain.main(StudentMain.java:16)
Process finished with exit code 1
Here is the code I believe is causing the error
import model.Person;
import utility.WorkbookUtility;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class StudentMain {
public final static String INPUT_FILE ="scr\\main\\webapp\\assets\\JavaWebProgramming.xlsx";
public static void main(String[] args) {
final File inputFile = new File(INPUT_FILE);
try {
final List<Person> people = WorkbookUtility.retrievePeopleFromWorkbook(inputFile);
for(final Person person : people) {
System.out.println(person);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Related
On the same directory of my Main.java file, I have a package/folder named database, and inside the database package I have a file named Data.txt.
This is my code of Main.java, but it is throwing this error:
java: exception java.io.FileNotFoundException
How can I get the file from a relative file? I'm used to web development, and usually something with a . dot like "./folder/file.txt" works.
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class Main {
public static void main(String[] args) {
readFile();
}
public static void readFile() {
File file = new File("./database/Data.txt");
Scanner scanner = new Scanner(file);
try {
while (scanner.hasNextLine()) {
int i = scanner.nextInt();
System.out.println(i);
}
scanner.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
You are not importing FileNotFoundException class. also, scanner statement throws the exception which should inside try. Solution is as below.
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class Main {
public static void main(String[] args) {
readFile();
}
public static void readFile() {
File file = new File("database/Data.txt");
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
int i = scanner.nextInt();
System.out.println(i);
}
scanner.close();
}catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Only check if those content can read using scanner or not. Content having int properly. otherwise it will throw java.util.InputMismatchException.
Are you working on a mac or windows system.
I am on windows and ".\database\Data.txt" would most probably work depending on where the file is in your file structure.
can someone tell me the error in my SnakeYaml usage v1.19. Somehow it cannot serialize the object and I do not know why.
my JavaBeanObject.java
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
public class ConfigRunner {
private static File myfile = new File(Paths.get(System.getProperty("user.home"),"config", "trial.yml").toString());
private static String mypath =myfile.getPath();
private JavaBeanObject myparam;
public static JavaBeanObject getConfiguration(String filePath) throws IOException{
Constructor constructor = new Constructor(JavaBeanObject.class);
Yaml yaml = new Yaml(constructor);
try (InputStream in = Files.newInputStream(Paths.get(filePath))) {
JavaBeanObject config = yaml.loadAs(in, JavaBeanObject.class);
System.out.println(config.toString());
return config;
}
}
public static void main(String[] args) {
try {
System.out.println(mypath);
getConfiguration(mypath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
My ConfigRunner:
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
public class ConfigRunner {
private static File myfile = new File(Paths.get(System.getProperty("user.home"),"config", "trial.yml").toString());
private static String mypath =myfile.getPath();
private JavaBeanObject myparam;
public static JavaBeanObject getConfiguration(String filePath) throws IOException{
Constructor constructor = new Constructor(JavaBeanObject.class);
Yaml yaml = new Yaml(constructor);
try (InputStream in = Files.newInputStream(Paths.get(filePath))) {
JavaBeanObject config = yaml.loadAs(in, JavaBeanObject.class);
System.out.println(config.toString());
return config;
}
}
public static void main(String[] args) {
try {
System.out.println(mypath);
getConfiguration(mypath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
My trial.yml
First: anton
Second: berta
Third: caesar
Fourth: Dora
Ok, the Problem I am facing here is with the Stringbuilder and it cannot serialize the object. The error that I get is:
C:\Users\domino\config\trial.yml
Exception in thread "main" Cannot create property=First for JavaBean=First: null
First: null
Second: null
Third: null
Fourth: null
in 'reader', line 1, column 1:
First: 'anton ...
^
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:313)
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.construct(Constructor.java:190)
at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:346)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:182)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:141)
at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:127)
at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:450)
at org.yaml.snakeyaml.Yaml.loadAs(Yaml.java:444)
at ConfigRunner.getConfiguration(ConfigRunner.java:26)
Caused by: org.yaml.snakeyaml.error.YAMLException: Unable to find property 'First' on class: JavaBeanObject
at org.yaml.snakeyaml.introspector.PropertyUtils.getProperty(PropertyUtils.java:132)
at org.yaml.snakeyaml.introspector.PropertyUtils.getProperty(PropertyUtils.java:121)
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.getProperty(Constructor.java:323)
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:241)
at ConfigRunner.main(ConfigRunner.java:36)
Can someone help explain the error?
This question already has an answer here:
Apache POI Parsing error
(1 answer)
Closed 7 years ago.
I am getting the below Exception when I am trying to read an Excel file:
Exception in thread "main" java.lang.IllegalAccessError: tried to access method org.apache.poi.util.POILogger.log(ILjava/lang/Object;)V from class org.apache.poi.openxml4j.opc.PackageRelationshipCollection
at org.apache.poi.openxml4j.opc.PackageRelationshipCollection.parseRelationshipsPart(PackageRelationshipCollection.java:304)
at org.apache.poi.openxml4j.opc.PackageRelationshipCollection.<init>(PackageRelationshipCollection.java:156)
at org.apache.poi.openxml4j.opc.PackageRelationshipCollection.<init>(PackageRelationshipCollection.java:124)
at org.apache.poi.openxml4j.opc.PackagePart.loadRelationships(PackagePart.java:559)
at org.apache.poi.openxml4j.opc.PackagePart.<init>(PackagePart.java:112)
at org.apache.poi.openxml4j.opc.PackagePart.<init>(PackagePart.java:83)
at org.apache.poi.openxml4j.opc.PackagePart.<init>(PackagePart.java:128)
at org.apache.poi.openxml4j.opc.ZipPackagePart.<init>(ZipPackagePart.java:78)
at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:218)
at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:662)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:223)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:186)
at readAndWriteToExcel.ReadingExcel.main(ReadingExcel.java:36)
My code is:
package readAndWriteToExcel;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
public class ReadingExcel {
private static final Log LOG = LogFactory
.getLog(ReadingExcel.class);
public static void main(String[] args) throws Exception {
String SAMPLE_PERSON_DATA_FILE_PATH = "C:/Users/Documents/Test Data 5.xlsx";
File file = new File(SAMPLE_PERSON_DATA_FILE_PATH);
InputStream inputStream = new FileInputStream(file);
// The package open is instantaneous, as it should be.
OPCPackage pkg = null;
try {
ExcelWorkSheetRowCallbackHandler sheetRowCallbackHandler = new ExcelWorkSheetRowCallbackHandler(
new ExcelRowContentCallback() {
public void processRow(int rowNum, Map<String, String> map) {
// Do any custom row processing here, such as save
// to database
// Convert map values, as necessary, to dates or
// parse as currency, etc
System.out.println("rowNum=" + rowNum + ", map=" + map);
}
});
pkg = OPCPackage.open(inputStream);
ExcelSheetCallBack sheetCallback = new ExcelSheetCallBack() {
private int sheetNumber = 0;
public void startSheet(int sheetNum) {
this.sheetNumber = sheetNum;
System.out.println("Started processing sheet number=" + sheetNumber);
}
public void endSheet() {
System.out.println("Processing completed for sheet number=" + sheetNumber);
}
};
System.out.println("Constructor: pkg, sheetRowCallbackHandler, sheetCallback");
ExcelReader example1 = new ExcelReader(pkg,
sheetRowCallbackHandler, sheetCallback);
example1.process();
System.out.println("nConstructor: filePath, sheetRowCallbackHandler, sheetCallback");
ExcelReader example2 = new ExcelReader(SAMPLE_PERSON_DATA_FILE_PATH, sheetRowCallbackHandler, sheetCallback);
example2.process();
System.out.println("nConstructor: file, sheetRowCallbackHandler, sheetCallback");
ExcelReader example3 = new ExcelReader(file,
sheetRowCallbackHandler, null);
example3.process();
} catch (RuntimeException are) {
LOG.error(are.getMessage(), are.getCause());
} catch (InvalidFormatException ife) {
LOG.error(ife.getMessage(), ife.getCause());
} catch (IOException ioe) {
LOG.error(ioe.getMessage(), ioe.getCause());
} finally {
IOUtils.closeQuietly(inputStream);
try {
if (null != pkg) {
pkg.close();
}
} catch (IOException e) {
// just ignore IO exception
}
}
}
}
Is there a simple way to read and edit an Excel file (xls and xlsx) with more than 50k records? I have searched a lot and worked with a few available codes.
But I was not successful, I keep ending up with one Exception or another.
I was getting the same error:
"Handler processing failed; nested exception is
java.lang.IllegalAccessError: tried to access method
org.apache.poi.util.POILogger.log(ILjava/lang/Object;)V from class
org.apache.poi.openxml4j.opc.PackageRelationshipCollection"
After updating maven and trying to access PackageRelationshipCollection and POILogger classes in eclipse things worked fine for me. Make sure you have all the required jars i.e poi, poi-ooxml, poi-ooxml-schemas.
I wanted to make a way so that I can load all the Images in my source folder without having to code each line for each image, but I keep getting an error and have tried different ways to do this and I still can't figure it out. Is it just impossible?
Here is how it works: I save all the images I am going to need on a text file (in this case startUp.txt), then I store the lines in the text file in a linked-list(String) then I use a loop to get the images and store those in a linked-list(Image) so that I don't need to write code for every single Image to load.
Here is my code:
package com.game.task;
import java.awt.Image;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.Scanner;
import javax.swing.ImageIcon;
public class imagesTest {
public LinkedList<Image> storeImages = new LinkedList<Image>();
private LinkedList<String> storeStrings = new LinkedList<String>();
public imagesTest() {
load();
readimage();
}
public void readimage() {
for(int index = 0; index < storeStrings.size(); index++){
Image temp = new ImageIcon(this.getClass().getClassLoader().getResource("/res/"+storeStrings.get(index))).getImage();
storeImages.add(temp);
}
}
private void load() {
File file = new File("Data/startUp.txt");
try {
Scanner read = new Scanner(file);
while (read.hasNextLine()) {
storeStrings.add(read.nextLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] arsg) {
imagesTest t = new imagesTest();
}
}
Exception:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
at com.game.task.imagesTest.readimage(imagesTest.java:24)
at com.game.task.imagesTest.load(imagesTest.java:41)
at com.game.task.imagesTest.<init>(imagesTest.java:17)
at com.game.task.imagesTest.main(imagesTest.java:45)
I found a way to loop through the resource folder and it was not that hard.
All i had to do use make a method and keep calling it in a loop here is the code
package com.game.task;
import java.awt.Image;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.Scanner;
import javax.swing.ImageIcon;
public class imagesTest {
public LinkedList<Image> storeImages = new LinkedList<Image>();
private LinkedList<String> storeStrings = new LinkedList<String>();
public imagesTest() {
load();
start();
}
public void readimage(String f) {
Image temp = new ImageIcon(this.getClass().getResource("/res/"+f)).getImage();//looks for it
storeImages.add(temp);//put it in a linked-List(Image)
}
public void start(){
for(int index = 0; index < storeStrings.size(); index++){
readimage(storeStrings.get(index));//calling the methode to look in resource folder
}
}
//reads the text file and puts the out put in a linked-list(String)
private void load() {
File file = new File("Data/startUp.txt");
try {
Scanner read = new Scanner(file);
while (read.hasNextLine()) {
storeStrings.add(read.nextLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] arsg) {
imagesTest t = new imagesTest();
}
}
The following program is meant to read from a file:-
import java.util.*;
import java.io.*;
import java.lang.*;
public class file_read
{
private Scanner input = new Scanner(System.in);
private Scanner fileinput;
public void open() // [1]
{
try
{
String filename = input.next();
fileinput = new Scanner(new File(filename+".txt")); // [2]
}
catch(Exception e)
{
System.out.println("opening error.");
}
}
public void read()
{
// some task to read the file
}
public void closeFile()
{
// closing the file
}
}
Problem lies with [1] and I think that the statement [2] is creating the problem. If I replace the filename+".txt" with the actual filename, everything runs fine. I am unable to pinpoint the reason. Please help.