Can't insertRow which contains Strings from file [duplicate] - java

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.

Related

Why do I get ArrayIndexOutOfBoundsException: 1? [duplicate]

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+");

How to read a textfile and saving it into an Array? [duplicate]

This question already has answers here:
Java: Reading a file into an array
(5 answers)
Closed 4 years ago.
i would like to build a text data-cleaner in Java, which
cleans the text from Smileys and other special charakter. I wrote a text reader,
but he stops after 3/4 of Line 97 and i just don't know why he does it? Normally he should read the complete text file (ca. 110.000 Lines) and then stop. It would be really nice if could show me where my mistake is.
public class FileReader {
public static void main(String[] args) {
String[] data = null;
int i = 0;
try {
Scanner input = new Scanner("C://Users//Alex//workspace//Cleaner//src//Basis.txt");
File file = new File(input.nextLine());
input = new Scanner(file);
while (input.hasNextLine()) {
String line = input.nextLine();
System.out.println(line);
data[i] = line;
i++;
}
input.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
System.out.println(data[97]);
}
}
Your mistake is here:
String[] data = null;
I would expect this code to throw null pointer exception...
You can use ArrayList instead of plain array if you want to have dynamic re-sizing

What is causing null pointer exception at runtime? [duplicate]

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.

NullPointerException when writing csv file [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 8 years ago.
I want to write a csv with the opencsv libary, however, when running the code I get a NullPointerException.
public void exportToCSV(ArrayList<Data> list) throws IOException {
log.info("write CSV file");
String writerPath = "C:\\Users\\User\\Desktop\\Output\\output.csv";
CSVWriter writer = new CSVWriter(new FileWriter(writerPath), ';');
//headers
String [] entries = {"ID", "Date"};
writer.writeNext(entries);
List<String[]> data = new ArrayList<String[]>();
for (int m = 0; m < list.size(); m++) {
data.add(new String[] {
list.get(m).getID,
(list.get(m).getDate().toString()==null) ? "null" : list.get(m).getDate().toString(), //Here i get the NullPointerException
});
}
writer.writeAll(data);
writer.close();
}
I guess that getDate() is null, which type is a Timestamp. However, why does my proposed solution not work in writing a String when getDate() is null.
I apprecaite your reply!
list.get(m).getDate().toString()==null should be changed to list.get(m).getDate()==null.
If list.get(m).getDate() is null. Invoking a method on it will cause NullPointerException.

Populating JCombobox with a text file [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I populate JComboBox from a text file?
I am new to programming Java with only 2 months of experience. Can anyone help me to populate a JComboBox with a text file, consisting of 5 lines? I have looked at code on Google, but I keep getting errors.
private void populate() {
String[] lines;
lines = readFile();
jComboBox1.removeAllItems();
for (String str : lines) {
jComboBox1.addItem(str);
}
}
Here is readFile(). From this site
private String[] readFile() {
ArrayList<String> arr = new ArrayList<>();
try {
FileInputStream fstream = new FileInputStream("textfile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
while ((strLine = br.readLine()) != null) {
arr.add(strLine);
}
in.close();
} catch (Exception e) {
}
return arr.toArray(new String[arr.size()]);
}

Categories