samedi 7 juin 2014

Keep getting NullPointerException java...why?? How do I fix this?


Vote count:

0




So I'm almost finished my assignment that reads a textfile of "Tweets" and then makes objects out of each line, finding hashtags and links etc., and then prints out the tweets all nice and formatted, and it seems to be working only when I run the program it only prints out the first tweet and then throws some NullPointerException thing that looks like this:



java.lang.NullPointerException
at Tweet.getReferences(Tweet.java:99)
at Tweet.toString(Tweet.java:144)
at A2Q2.printTweets(A2Q2.java:73)
at A2Q2.main(A2Q2.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)


I thought my toString() method in my Tweet class would work around the null problem, but apparently not! I don't know what I'm doing wrong, and some help would be greatly appreciated!! Here's my code and the .txt file:


Worker class:



import java.io.*;

public class A2Q2
{
public static void main(String[] args)
{
A0 print = new A0("A2Q2", "2", "2", "Thursday, June 5th, 2014\n");
Tweet[] tweets = extractRecords("A2Q2in.txt");
printTweets(tweets);

System.out.println("\nProgram completed normally.");
}

public static Tweet[] extractRecords(String filename)
{
BufferedReader fileIn;
String inputLine;
String[] line = new String[3];
int count = 0;
int MAX_RECORDS = 100;
String user;
String text;
int retweets;
int favourites;
Tweet[] tweetArray = new Tweet[MAX_RECORDS];

try
{
fileIn = new BufferedReader(new FileReader(filename));
inputLine = fileIn.readLine();
while(inputLine != null)
{
line = inputLine.split(",");
user = line[0];
text = line[1];
retweets = Integer.parseInt(line[2]);
favourites = Integer.parseInt(line[3]);

Tweet tweet = new Tweet(user, text, retweets, favourites);

tweetArray[count] = tweet;
count++;
inputLine = fileIn.readLine();
}
fileIn.close();
}

catch(IOException ioe)
{
ioe.printStackTrace();
}

Tweet[] trimmedTweetArray = new Tweet[count];
System.arraycopy(tweetArray, 0, trimmedTweetArray, 0, count);

return trimmedTweetArray;
}

public static void printTweets(Tweet[] tweets)
{
int count = 0;
while(count < tweets.length)
{
System.out.println(tweets[count].toString());
System.out.println();

count++;
}
}

}


The Tweet object class:



import java.util.Arrays;

public class Tweet
{
private static final String hashtag = "#";
private static final String atSign = "@";
private static final String dotCom = ".com";

private String user;
private String text;
private int retweets;
private int favourites;
private String[] otherUsers;
private String[] links;
private String[] hashtags;


public Tweet(String user, String text, int retweets, int favourites)
{
this.user = user;
this.text = text;
this.retweets = retweets;
this.favourites = favourites;
otherUsers = null;
links = null;
hashtags = null;

if(text.contains(hashtag))
{
hashtags = extract(text, hashtag);
}

if(text.contains(atSign))
{
otherUsers = extract(text, atSign);
}

if(text.contains(dotCom))
{
links = extract(text, dotCom);
}
}


public String getUser()
{
return user;
}


public String getText()
{
return text;
}


public int getRetweets()
{
return retweets;
}


public int getFavourites()
{
return favourites;
}


public String getHashtags()
{
String returnString = "\tHashtags:";
String add;
int count = 0;

while(count < hashtags.length)
{
returnString += "\n\t\t";
add = String.format("%-25s", hashtags[count]);
count++;
returnString = returnString + add;
}

if(hashtags == null)
{
returnString = null;
}

return returnString;

}


public String getReferences()
{
String returnString = "\tReferences:";
String add;
int count = 0;

while(count < otherUsers.length)
{
returnString += "\n\t\t";
add = String.format("%-25s", otherUsers[count]);
count++;
returnString = returnString + add;
}

if(otherUsers == null)
{
returnString = null;
}

return returnString;

}


public String getLinks()
{
String returnString = "\tLinks:";
String add;
int count = 0;

while(count < links.length)
{
returnString += "\n\t\t";
add = String.format("%-20s", links[count]);
count++;
returnString = returnString + add;
}

if(links == null)
{
returnString = null;
}

return returnString;

}


public String toString()
{
String returnString = null;
String references_ = getReferences();
String links_ = getLinks();
String hashtags_ = getHashtags();

returnString = String.format("Tweet text: %s \n\t%-27s Retweets:%6d Favourited:%6d ", getText(), getUser(), getRetweets(), getFavourites());
returnString += "\n";
if(references_ != null)
{
returnString += references_ + "\n";

}
if(links_ != null)
{
returnString += links_ + "\n";
}
if(hashtags_ != null)
{
returnString += hashtags_ + "\n";
}

return returnString;
}


public static String[] extract(String text, String constant)
{
int MAX_VALUE = 100;
String[] textArray = text.split(" ");
String[] constantArray = new String[MAX_VALUE];
int count = 0;
int count2 = 0;

while(count < textArray.length)
{
if(textArray[count].contains(constant))
{
constantArray[count2] = textArray[count];
count2++;
}
count++;
}
String[] trimmedConstantArray = new String[count2];
System.arraycopy(constantArray, 0, trimmedConstantArray, 0, count2);

return trimmedConstantArray;
}
}


And the .txt file:



@outlander_starz, In honor of the very first release of @Writer_DG's #Outlander June 1st is officialy #WorldOulanderDay http://ift.tt/1p7T4zg
@welovehistory, Happy #worldoutlanderday from all at Historic Scotland (but specifically Doune & Blackness Castles!) See you all soon we hope!,55,124
@thatericalper, ThatEricAlper's show on Channel 167 Canada Talks today 7:00pmET: @Darryl_Sterdan on Led Zep reissues and sets from catl and Skydiggers!,14,8
@theonion, Top Story: VA To Improve Veterans’ Health Care With New $500 Million Waiting Room http://onion.com/1hH2euG ,14,8
@cbcnews, Bodies of 6 Mount Rainier climbers may not be recovered http://bit.ly/RUwATy http://ift.tt/1p7T4zi
@wilw, I'm reading comic books. Anne's reading Harry Potter. I think there's ice cream in our future. #grownups,76,433
@jeanne_beker, Gus drives home from the cottage in style!!! http://ift.tt/1p7T6Hx
@farmerybrewery, @DowntownWpgBIZ enjoy the evening and beer gang! Events like these makes us so proud of being #local #craftbeer #winnipeg,3,4
@wilw, “You could lie down someplace that’s not in the sun but I don’t know why you would.” - from A Guide To Being A Dog by Seamus Wheaton,52,131


Thanks for any help! :)



asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire