Keep getting a null pointer exception for some odd reasonim creati.pdf
1. Keep getting a null pointer exception for some odd reason
im creating the variable and the object but it still isnt working
My code is below please help
package classes;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class NewClass4
{
private class Node
{
private String text;
private int Hit;
private int Miss;
private Node next;
public Node(String text, int Hit, int Miss, Node next)
{
this.text=text;
this.Hit=Hit;
this.Miss=Miss;
this.next=next;
}
public void incrementHit()
{
this.Hit = this.Hit+1;
}
public void incrementMiss()
2. {
this.Miss = this.Miss+1;
}
public String getText()
{
return this.text;
}
public int getHit()
{
return this.Hit;
}
public int getMiss()
{
return this.Miss;
}
}//end of node
private class LL
{
public Node head;
public LL()
{
head=null;
}
public void insert(String text2, boolean isHit)
{
System.out.println("entered insert method");
Node temp=this.head;
System.out.println("sent temp node to head");
while((temp!=null) && (!temp.text.equals(text2)) )
{
temp=temp.next;
}
if(temp==null)
{
// we did not find the string
if(isHit)
3. {
this.head=new Node(text2,1, 0, this.head );
}
else
{
this.head=new Node(text2,0, 1, this.head );
}
}
else
{
if(isHit)
{
temp.incrementHit();
}
else
{
temp.incrementMiss();
}
}
}//end of insert
public void printList()
{
Node temp=this.head;
while(temp!=null)
{
int Hit=temp.getHit();
int Miss=temp.getMiss();
double sucRate=Hit*100/(Hit+Miss);
System.out.println(temp.getText() + " " + sucRate);
}
}
}// end of LL
private static LL myLL;
public NewClass4()
4. {
this.myLL = new LL();
}
public static void main(String[] args)
{
SimpleDateFormat sdf = new
SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss",Locale.ENGLISH);
String myString = null;
Pattern a = Pattern.compile("[0-9]{2}/?[A-Za-z]{3,4}/?[0-9]{4}[.,-:]{1}[0-9.,-
:]{1,}s+");
Pattern d= Pattern.compile("s+/+[0-9.]{3}/+[A-Za-z]{1,}/+[A-Za-z.,-:%]{1,}");
Pattern f= Pattern.compile("s+[0-9]{3}s+");
String newLine;
try {
File file = new File("myFile.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null)
{
myString = line;
System.out.println(myString);
Matcher m1 = a.matcher(myString);
Matcher m2 = d.matcher(myString);
Matcher m3 = f.matcher(myString);
if (m1.find() && m2.find()&& m3.find())
try {
Date parsedDate = sdf.parse(m1.group());
SimpleDateFormat print = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
int responseCode = Integer.parseInt(m3.group().trim());
System.out.println(print.format(parsedDate)+ " "+m2.group()+" "+responseCode);
newLine = print.format(parsedDate)+ " "+m2.group();
// decide true or false
if (responseCode >= 500 )
{
5. if (NewClass4.myLL == null)
{
System.out.println("Link List is empty");
}
NewClass4.myLL.insert(newLine, false);
}
else
{
NewClass4.myLL.insert(newLine, true);
}
} // end of second try
catch (ParseException e)
{
e.printStackTrace();
}
}// end of while
fileReader.close();
//System.out.println("Contents of file:");
}//end of first try
catch (IOException e)
{
e.printStackTrace();
}
//Classes.myLL.printList();
}// end of main
}// end of Classes
Solution
package classes;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
6. import java.io.*;
import java.util.*;
import java.text.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class NewClass4 {
private class Node {
private String text;
private int Hit;
private int Miss;
private Node next;
public Node(String text, int Hit, int Miss, Node next) {
this.text = text;
this.Hit = Hit;
this.Miss = Miss;
this.next = next;
}
public void incrementHit() {
this.Hit = this.Hit + 1;
}
public void incrementMiss() {
this.Miss = this.Miss + 1;
}
public String getText() {
return this.text;
}
public int getHit() {
return this.Hit;
}
public int getMiss() {
return this.Miss;
}
}// end of node
private class LL {
public Node head;
public LL() {
7. head = null;
}
public void insert(String text2, boolean isHit) {
System.out.println("entered insert method");
Node temp = this.head;
System.out.println("sent temp node to head");
while ((temp != null) && (!temp.text.equals(text2))) {
temp = temp.next;
}
if (temp == null) {
// we did not find the string
if (isHit) {
this.head = new Node(text2, 1, 0, this.head);
} else {
this.head = new Node(text2, 0, 1, this.head);
}
} else {
if (isHit) {
temp.incrementHit();
} else {
temp.incrementMiss();
}
}
}// end of insert
public void printList() {
Node temp = this.head;
while (temp != null) {
int Hit = temp.getHit();
int Miss = temp.getMiss();
double sucRate = Hit * 100 / (Hit + Miss);
System.out.println(temp.getText() + " " + sucRate);
}
}
}// end of LL
private static LL myLL;
public NewClass4() {
8. this.myLL = new LL();
}
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss",
Locale.ENGLISH);
String myString = null;
Pattern a = Pattern
.compile("[0-9]{2}/?[A-Za-z]{3,4}/?[0-9]{4}[.,-:]{1}[0-9.,-:]{1,}s+");
Pattern d = Pattern
.compile("s+/+[0-9.]{3}/+[A-Za-z]{1,}/+[A-Za-z.,-:%]{1,}");
Pattern f = Pattern.compile("s+[0-9]{3}s+");
String newLine;
try {
File file = new File("myFile.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
myString = line;
System.out.println(myString);
Matcher m1 = a.matcher(myString);
Matcher m2 = d.matcher(myString);
Matcher m3 = f.matcher(myString);
if (m1.find() && m2.find() && m3.find())
try {
Date parsedDate = sdf.parse(m1.group());
SimpleDateFormat print = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm");
int responseCode = Integer.parseInt(m3.group().trim());
System.out.println(print.format(parsedDate) + " "
+ m2.group() + " " + responseCode);
newLine = print.format(parsedDate) + " " + m2.group();
// decide true or false
if (responseCode >= 500) {
if (NewClass4.myLL == null) {
System.out.println("Link List is empty");
9. }
NewClass4.myLL.insert(newLine, false);
} else {
NewClass4.myLL.insert(newLine, true);
}
} // end of second try
catch (ParseException e) {
e.printStackTrace();
}
}// end of while
fileReader.close();
// System.out.println("Contents of file:");
}// end of first try
catch (IOException e) {
e.printStackTrace();
}
// Classes.myLL.printList();
}// end of main
}// end of Classes
myFile.txt
2011-06-14T04:12
2013-06-14T04:12
2012-06-14T04:12
Note: Please make sure the path of the file is in the project location, i think that is the problem