Results 1 to 2 of 2
Thread: Stupid mistake, maybe?
- 11-10-2011, 07:04 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 1
- Rep Power
- 0
Stupid mistake, maybe?
Hi, I'm trying to make a simple program that will read through a list and check and remove any repeated lines. However, it only reads and checksthe first line before getting a NullPointerException error while checking it under the "contains" void. Help is appreciated, thanks.Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package samething1; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.List; /** * * @author JSM */ public class SameThing1 { static File in; static String temp; static File out; static String[] Write; static String[] list; static int lastint = 0; /** * @param args the command line arguments */ public static void main(String[] args) throws InterruptedException, FileNotFoundException, IOException { System.out.println("Started"); Thread.sleep(5000); in = new File(getCurrentPath() + File.separator + "in1.txt"); out = new File(getCurrentPath() + File.separator + "out1.txt"); if (!in.exists()) { System.out.println("in1.txt doesn't exist"); System.out.println(in.toString()); Thread.sleep(5000); System.exit(0); } else { String tstring; BufferedReader ini = new BufferedReader(new FileReader(in)); boolean done = false; try { while (!done) { tstring = ini.readLine(); System.out.println("Read: " + tstring); if (!contains(tstring)) { add(tstring); } else {System.out.println(tstring + " has already been found, skipping"); } } } catch (Exception Ex) { Ex.printStackTrace(); done = true; System.out.println("Reached end of list. Writing output"); BufferedWriter outs = new BufferedWriter(new FileWriter(out)); int tint = 0; int tint2 = 0; if (out.exists()) {out.delete();} try { while (tint != tint2) { outs.write(Write[tint2]); outs.newLine(); tint2++; } } catch (Exception ex2) {} } } } public static String getCurrentPath() { try { temp = new File(".").getCanonicalPath(); System.out.println(temp); } catch (IOException e) { return null; } return temp; } public static void add(String str) { lastint++; list[lastint] = str; } public static boolean contains(String str) { for (String tstr : list) { if (str.equals(tstr)) { return true; } } return false; } }
- 11-10-2011, 08:39 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Similar Threads
-
How to reverse something really stupid I did... HELP PLEASE
By efebatistaarda in forum NetBeansReplies: 0Last Post: 02-15-2011, 11:16 PM -
I need an idea with something really stupid
By blf_titi in forum Threads and SynchronizationReplies: 2Last Post: 10-27-2010, 03:24 AM -
Stupid error
By dewitrydan in forum Java AppletsReplies: 3Last Post: 08-09-2010, 01:29 PM -
incredibally stupid....
By tek0011 in forum New To JavaReplies: 7Last Post: 11-26-2009, 09:35 AM -
a really stupid question
By SwinGirl in forum NetBeansReplies: 10Last Post: 06-25-2008, 09:06 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks