This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
My code compiles, but I get this error at runtime:
Exception in thread "main" java.lang.ExceptionInInitializerError
at GUI.<init>(GUI.java:46)
at GUI.main(GUI.java:252)
Caused by: java.lang.NullPointerException
at java.util.Objects.requireNonNull(Objects.java:203)
at java.util.Arrays$ArrayList.<init>(Arrays.java:3813)
at java.util.Arrays.asList(Arrays.java:3800)
at db.<clinit>(db.java:23)
... 2 more
This is the code causing the error:
public static String strLine;
public static String[] filearray;
public static List<String> list = Arrays.asList(filearray);
public static void load() throws IOException{
FileInputStream in = new FileInputStream("slist.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
filearray = new String[4];
while ((strLine = br.readLine()) != null) {
for (int j = 0; j < filearray.length; j++){
filearray[j] = br.readLine();
}
}
in.close();
Line 46 referenced in the error:
JList stafflist = new JList(db.list.toArray());
I am trying to load a text file as an Array and add it to the JList stafflist, but I get a runtime error.
It's in your declaration:
public static String[] filearray;
public static List<String> list = Arrays.asList(filearray);
fileArray is null, and you're trying to create a List around it.
You can either initialize fileArray right here:
public static String[] filearray = new String[4];
Or call Arrays.asList(filearray) after putting the values into filearray.
The problem that I see in your code is here:
public static String[] filearray;
public static List<String> list = Arrays.asList(filearray);
You are trying to convert filearray that is null at the time to list using Arrays.asList() method.
You should initialize filearray first.
Related
This question already has answers here:
ArrayIndexOutOfBoundsException for String.split()
(2 answers)
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 1 year ago.
I got ArrayIndexOutOfBoundsException: 1 at this line String name = pieces[1]; in the following function:
public static void loadFile() throws FileNotFoundException, IOException {
file = new File("C:\\Users\\DELL\\OneDrive - Philadelphia University\\Desktop\\NetBeansProjects\\CaloriesIntakeApp\\src\\main\\webapp\\WEB-INF\\data\\data.txt");
try (BufferedReader inputStream = new BufferedReader(new FileReader(file))) {
String line;
while ((line = inputStream.readLine()) != null) {
String[] pieces = line.split(" ");
String id = pieces[0];
String name = pieces[1];
int grms = Integer.parseInt(pieces[2]);
int calories = Integer.parseInt(pieces[3]);
String photo = pieces[4];
Fruit f = new Fruit(id, name, grms, calories, photo);
fruitList.add(f);
}
}
}
Need to know the reason!
Check if your data is clean in the file (data.txt). It will not work if there is a single empty line or basically any line not in the desired format.
Example:
a12387beac8 Apple 1 1500 /location/photo.jpeg
I hope you are trying to split based on white space.
try this:
String[] pieces = line.split("\\s+");
This question already has answers here:
How do I print my Java object without getting "SomeType#2f92e0f4"?
(13 answers)
Why does println(array) have strange output? ("[Ljava.lang.String;#3e25a5") [duplicate]
(6 answers)
Closed 3 years ago.
I'm trying to read from file and insert in a row, after inserting I'm having this :
[Ljava.lang.String;#7f059c04
[Ljava.lang.String;#855c8af
The Listener, and BufferRead and Inserting file read.
String filepath = "C:\\imdoingjava\\Java3-Projet\\valuesarticles.txt";
File file = new File(filepath);
private class okListener implements ActionListener{
public void actionPerformed(ActionEvent event){
try{
BufferedReader br = new BufferedReader(new FileReader(file));
Object [] tableLines = br.lines().toArray();
for(int i=0; i<tableLines.length; i++){
String line = tableLines[i].toString().trim();
String [] dataRow = line.split("\n");
model.insertRow(0, new Object[] {"", "", dataRow ,""});
}
}
catch(FileNotFoundException fo){
fo.getMessage();
}
}
If I used model.insertRow(0, dataRow); which I commented it, it works but it inserts it in the first column, and I want it in the third.
This question already has answers here:
Error message "unreported exception java.io.IOException; must be caught or declared to be thrown" [duplicate]
(5 answers)
Closed 4 years ago.
My code for reading an input stream of numbers and sorts them into an array, returns
error:
unreported exception IOException; must be caught or declared to be thrown
on String lines = br.readLine();
public int[] inputArr() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String lines = br.readLine();
String[] strs = lines.trim().split("\\s+");
int [] a = new int [strs.length];
for (int i = 0; i < strs.length; i++) {
a[i] = Integer.parseInt(strs[i]);
}
return a ;
}
Any help please on this?
BufferedReader.readLine() throws an IOException:
public String readLine() throws IOException {
So any call to it must handle it by either:
Surrounding with try/catch block
Declaring that your method also throws that exception.
Sorry to bother with a nullpointerexception question but this should be relatively easy and I don´t understand what´s causing it.
Basically there is a database called "andmebaas.txt" where all the data is separated by "###" and it should separate the information and display the ones I have requested (m2ng[0].nimi and m2ng[10].nimi)
import java.io.*;
import java.util.*;
public class kt_5_1 {
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner1 = new Scanner(new File("andmebaas.txt"));
Scanner scanner2 = new Scanner(new File("andmebaas.txt"));
int n;
for(n = 0; scanner1.hasNext(); n++) {scanner1.nextLine();}
m2ngud m2ng[] = new m2ngud[n];
for (int c = 0; scanner2.hasNext(); c++) {
String wholeLine = scanner2.nextLine();
String[] line = wholeLine.split("###");
m2ng[c].nimi = line[0]; //this is line 17.
scanner2.nextLine();
}
System.out.println(m2ng[0].nimi);
System.out.println(m2ng[10].nimi);
}
}
public class m2ngud {
String nimi, kuup2ev, tootja, zanr, hinne;
}
the error:
Exception in thread "main" java.lang.NullPointerException
at kt_5_1.main(kt_5_1.java:17)
Thanks for all your answers in advance!
by
m2ngud m2ng[] = new m2ngud[n];
you allocated references, all of them are still referring to null, you need to initialize each element, like
m2ng[c] = new m2ng();
m2ng[c].nimi = line[0]; //this is line 17.
before accessing them
m2ng[c].nimi = line[0];
Before this, first create an object of m2ng class than access nimi variable of that m2ng class.
m2ng[c] = new m2ng();
This question already has answers here:
ClassFormatError in Java
(3 answers)
Closed 9 years ago.
I have one generated class which get this error. Inside this class, there is one huge static block (5000+ lines). I broke the block into several smaller static blocks but still got this error. Why is that
Edit
Code looks like:
private static final Map<Object, Object> nameMap = Maps.newHashMap();
static{
nameMap.put(xxx);
.... 5000 similar lines
nameMap.put(xxx);
}
If it is just data you will need to read the data in from a resource.
Arrange for your data file to be in the same location as the class file and use something like this:
class Primes {
private static final ArrayList<Integer> NUMBERS = new ArrayList<>();
private static final String NUMBER_RESOURCE_NAME = "numbers.txt";
static {
try (InputStream in = Primes.class.getResourceAsStream(NUMBER_RESOURCE_NAME);
InputStreamReader isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr)) {
for (String line; (line = br.readLine()) != null;) {
String[] numberStrings = line.split(",");
for (String numberString : numberStrings) {
if (numberString.trim().length() > 0) {
NUMBERS.add(Integer.valueOf(numberString));
}
}
}
} catch (NumberFormatException | IOException e) {
throw new IllegalStateException("Loading of static numbers failed", e);
}
}
}
I use this to read a comma separated list of 1000 prime numbers.