I am getting a weird NullPointerExcpetion in line 20:
regs[Integer.parseInt(str.split(" ")[1]) - 1].add(line.poll());
I do not know what has caused this. Can someone please help me fix this?
import java.io.*;
import java.util.*;
public class shoppay
{
public static void main (String[] args) throws IOException
{
BufferedReader f = new BufferedReader(new FileReader("shoppay.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("shoppay.out")));
Queue<Integer> line = new LinkedList <Integer>();
int num = Integer.parseInt(f.readLine());
String str;
LinkedList<Integer>[] regs = (LinkedList<Integer>[]) new LinkedList[num];
while ((str = f.readLine()) != null)
{
if (str.charAt(0) == 'C')
line.add(Integer.parseInt(str.split(" ")[1]));
else
regs[Integer.parseInt(str.split(" ")[1]) - 1].add(line.poll());
}
out.close();
System.exit(0);
}
}
Also, I am getting a warning:
Type safety: Unchecked cast from java.util.LinkedList[] to java.util.LinkedList[]
Does this have something to do with the error?
Edit: Input is just a string of lines. First line is a number, and the rest are either “C” or “R” followed by a number. Also, I need a queue for regs.
Don’t create an array of generic lists. For technical reasons, it doesn’t really work cleanly. It’s much better to use a list of lists:
As a side issue, is there any reason you’re using LinkedList for
regsinstead ofArrayList?