Solved: java NullPointerException when running the program

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

sillyboy

Thread Starter
Joined
May 30, 2007
Messages
27
It is a Java program I wrote, with so far no compile error. But there is the NullPointerException message when running the program. How to solve the problem?

Exception in thread "main" java.lang.NullPointerException
at TaskSet$Task.access$000(TaskSet.java:75)
at TaskSet.getStartTime(TaskSet.java:72)
at TaskSet.minCompletionTime(TaskSet.java:47)
at TaskSetTest.main(TaskSetTest.java:11)
Press any key to continue . . .

Here my program

import java.lang.Math;

public class TaskSet
{
private final int MAX = 20;
private Task tasks[];
private int matrix[][];
private int num;

public TaskSet()
{
tasks = new Task[MAX];
matrix = new int[MAX][MAX];
num = 0;
for (int i = 0; i < MAX; i++)
{
for (int j = 0; j < MAX; j++)
{
matrix[j] = 0;
}
}
}

public void addTask(int x)
{
tasks[num++] = new Task(x);
}

public void addTaskConstraint(int i, int j)
{
if (matrix[j] == 1)
{
tasks.startTime = -1;
tasks[j].startTime = -1;
return;
}
matrix[j] = 1;
getStartTime(i);
minCompletionTime();
}

public int minCompletionTime()
{
int time = 0;
for (int i = 0; i < MAX; i++)
{
if (getStartTime(i) == -1)
{
return -1;
}
time = Math.max(time, getStartTime(i) + tasks.time);
}
return time;
}

public int getStartTime(int i)
{
for (int j = 0; j < MAX; j++)
{
if (matrix[j] == 1 && tasks[j].startTime != -1)
{
for (int k = 0; k < MAX; k++)
{
if (matrix[j][k] == 1 && tasks[k].startTime != -1)
{
tasks[j].startTime = Math.max(tasks[j].startTime, tasks[k].startTime + tasks[k].time);
}
}
tasks.startTime = Math.max(tasks.startTime, tasks[j].startTime + tasks[j].time);
}
}
return tasks.startTime;
}

private static class Task
{
private int time;
private int startTime;

public Task(int x)
{
time = x;
startTime = 0;
}
}
}

public class TaskSetTest
{
public static void main(String[] args)
{
TaskSet tasks = new TaskSet();

tasks.addTask(8);
tasks.addTask(2);
tasks.addTask(5);

System.out.println(tasks.minCompletionTime());
tasks.addTaskConstraint(0, 2);
System.out.println(tasks.minCompletionTime());
tasks.addTaskConstraint(0, 1);
System.out.println(tasks.minCompletionTime());

System.out.println(tasks.getStartTime(0));
System.out.println(tasks.getStartTime(1));
System.out.println(tasks.getStartTime(2));

tasks.addTaskConstraint(1, 2);
System.out.println(tasks.minCompletionTime());
System.out.println(tasks.getStartTime(0));
System.out.println(tasks.getStartTime(1));
System.out.println(tasks.getStartTime(2));

tasks.addTaskConstraint(1, 0);
System.out.println(tasks.minCompletionTime());
System.out.println(tasks.getStartTime(0));
System.out.println(tasks.getStartTime(1));
System.out.println(tasks.getStartTime(2));
}
}
 
Joined
Dec 22, 2009
Messages
81
I was going to debug this for you but then I grew bored with that.
Just follow the standard procedure for figuring out what is wrong.
Add print statements, breakpoints etc.
Debugging is a valuable skill to know
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Members online

Top