Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Search Search
Search for:
Tech Support Guy > > >

Solved: java NullPointerException when running the program


(!)

sillyboy's Avatar
sillyboy sillyboy is offline
Junior Member with 27 posts.
THREAD STARTER
 
Join Date: May 2007
Experience: Intermediate
24-Apr-2010, 02:32 PM #1
Solved: java NullPointerException when running the program
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[i][j] = 0;
}
}
}

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

public void addTaskConstraint(int i, int j)
{
if (matrix[j][i] == 1)
{
tasks[i].startTime = -1;
tasks[j].startTime = -1;
return;
}
matrix[i][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[i].time);
}
return time;
}

public int getStartTime(int i)
{
for (int j = 0; j < MAX; j++)
{
if (matrix[i][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[i].startTime = Math.max(tasks[i].startTime, tasks[j].startTime + tasks[j].time);
}
}
return tasks[i].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));
}
}
e2e8's Avatar
e2e8 e2e8 is offline
Member with 81 posts.
 
Join Date: Dec 2009
24-Apr-2010, 10:03 PM #2
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
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
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.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Similar Threads
Title Thread Starter Forum Replies Last Post
Laptop slows or freezes when running java mmm_driftwood Virus & Other Malware Removal 1 19-Nov-2008 03:26 PM
Computer freezing when running media programs dj_bowser Windows XP 5 05-Jun-2005 06:51 PM
Machine crashes when running multiple programs chris in va Earlier Versions of Windows 3 11-Jan-2003 12:07 AM
I am receiving error mess. and freezes when running various programs gomu Earlier Versions of Windows 8 12-Sep-2001 06:41 PM
System hangs up when running Dos programs with Graphics klehma All Other Software 0 09-Feb-2000 09:17 AM

WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑

Content Relevant URLs by vBSEO 3.3.2