Ok so I have a function called gitBits() and it runs through its code. It contains a Scanner and a File. The file 100% exists because I made it. The function runs correctly if called from main() with the correct output. However, when I go to paint() and call the function, it tells me that there is a FileNotFoundException. To solve this, I put the called function into a try-catch statement and caught the exception. My code executes without any errors, but the problem is that my function getBits()'s return value is never assigned to my array of ArrayLists, arrayArray.
public class bitmaps extends JApplet{
public void init(){
getContentPane().setBackground(Color.red);
}
//This function reads from a bitmap file and stores the characters (0s and 1s) into arrayLists
public static ArrayList[] getBits() throws FileNotFoundException{
File bitmapFile = new File("bitmap.bmp");
Scanner reader = new Scanner(bitmapFile);
int numLines = 0;
String readStrings = "";
ArrayList<String> stringArray = new ArrayList<String>();
ArrayList<Character> onesZeros = new ArrayList<Character>();
do{
readStrings = reader.nextLine();
System.out.println(readStrings);
stringArray.add(readStrings);
numLines++;
for(char ch: readStrings.toCharArray()){
onesZeros.add(ch);
}
} while(reader.hasNextLine());
System.out.println(onesZeros);
reader.close();
ArrayList[] arrayArray = {stringArray, onesZeros};
return arrayArray;
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.black);
g.drawString("begin", 25, 25); //DRAWS THIS
ArrayList[] arrayArray = new ArrayList[2]; //THIS IS FINE
try{
g.drawString("1", 50, 50); //DRAWS THIS
arrayArray = getBits(); //DOESN'T EXECUTE THIS ASSIGNMENT
throw new FileNotFoundException();
} catch(FileNotFoundException e){
}
finally{
g.drawString("end", 75, 75); //DRAWS THIS
}
//SOMETHING I TRIED EARLIER, DOESN'T WORK
/*ArrayList[] arrayArray = getBits();
ArrayList<String> bitLines = arrayArray[0];
ArrayList<Character> onesZeros = arrayArray[1];
int x = 0;*/
/*for(char bit: onesZeros){
g.fillRect(0, 0, 10, 10);
}*/
}
public static void main(String[] args) throws FileNotFoundException{
}
}
So I throw the exception and everything, so everything should be fine. I just don't understand why it won't be assigned to the return value of getBits();
Try using an absolute path for bitmap.bmp
instead of a relative one.
Also,
try
{
g.drawString("1", 50, 50);
arrayArray = getBits();
throw new FileNotFoundException();
}
catch(FileNotFoundException e)
{
}
finally
{
g.drawString("end", 75, 75);
}
should be
try
{
g.drawString("1", 50, 50);
arrayArray = getBits();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
finally
{
g.drawString("end", 75, 75);
}