Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: java Error: NullPointerException

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java Error: NullPointerException

    Good day everyone. Can someone help me with my c program? I got stuck with this error whenever I run this program:
    thanks a lot.


    Example:

    Enter the no of blocks in the disk:--
    3

    Enter the size of each block:--
    3

    Enter the number of files:--
    3

    Enter the file of 1 name:--re
    Exception in thread "main" java.lang.NullPointerException
    at GlobalMembersFile_allocation.read(GlobalMembersFil e_allocation.java:94)
    at GlobalMembersFile_allocation.main(GlobalMembersFil e_allocation.java:310)


    Here is my code

    import java.util.*;
     
    public class GlobalMembersFile_allocation
    {
    	 public static list hf;
    	 public static list lf;
    	 public static list[] ha = new list[10];
    	 public static list[] la = new list[10];
    	public static int m;
    	public static int n;
    	public static int size;
    	 public static file[] f = new file[10];
    	public static index h;
    	public static index l;
     
     
    	public static int[] a = new int[200];
    	public static int g = 0;
     
     
    	public static void createfree()
    	{
    	 int i;
     
    	 //hf = (list)malloc(sizeof(list));		
    	 hf = new list();
    	 hf.next = null;
    	 for (i = 1;i <= m;i++)
    	 {
     
    	  list node = new list();
    	  node.datano = i;
    	  node.name = null;
    	  node.next = null;
    	  if (i == 1)
    	  hf.next = node;
    	  else
    	  lf.next = node;
    	  lf = node;
     
    	 }
    	}
     
     
    	  public static void createind()
    	  {
    	 int i;
     
     
    	 h = new index();
    	 h.next = null;
    	 for (i = 0;i < n;i++)
    	 {
     
     
    	  index node = new index();
     
    	  node.datano = i;
    	  node.name = null;
    	  node.next = null;
    	  node.link = null;
    	  if (i == 0)
    	  h.next = node;
    	  else
    	  l.next = node;
    	l = node;
    	 }
    	  }
     
    	public static void read()
    	{
     
    	 int i;
    	 System.out.print("\nEnter the no of blocks in the disk:--\n");	
     
    		m = sc.nextInt();
    	 System.out.print("\nEnter the size of each block:--\n");
     
    		size = sc.nextInt();
    	 System.out.print("\nEnter the number of files:--\n");
     
    	n = sc.nextInt();
    	 for (i = 0;i < n;i++)
    	 {
    	  System.out.printf("\nEnter the file of %d name:--",i + 1);
     
       f[i].name = sc.next();
     
    	  System.out.printf("\nEnter the size of file %d:--",i + 1);
    	 f[i].size = sc.nextInt();
     
    	  if (f[i].size % size == 0)
    	  f[i].nob = f[i].size / size;
    	  else
    	  f[i].nob = f[i].size / size+1;
    	 }
    	}
     
     
     
     
     
     
     
    	public static void contiguous()
    	{
    	 int i;
    	 int j;
    	 int g = 1;
    	 list temp;
    	 for (i = 0;i < n;i++)
    	 {
     
     
    ha[i]=new list();
    	  for (j = 0;j < f[i].nob;j++)
    	  {
     
     
    	  list node = new list();
    	   node.datano = g;
    	   g++;
    	   node.name = f[i].name;
    	   node.next = null;
    	   if (j == 0)
    		ha[i].next = node;
    		else
    		la[i].next = node;
    		la[i] = node;
    	  }
    	 }
    	temp = hf;
    	for (i = 0;i < g;i++)
    	temp = temp.next;
    	hf.next = temp;
    	}
     
     
    	public static void dele(int r)
    	{
    	list t;
    	for (t = hf;t != null;t = t.next)
    	if (t.next.datano == r)
    	{
    	t.next = t.next.next;
    	break;
    	}
    	}
     
     
    	public static void linked()
    	{
    	int r = 1;
    	int i;
    	int j;
    	int k = 0;
    	list t;
    	for (i = 0;i < n;i++)
    	{
     
     
    ha[i] = new list();
    	for (j = 0;j < f[i].nob;j++)
    	{
    	r = a[k];
    	k++;
    	GlobalMembersFile_allocation.dele(r);
     
     
    	list node = new list();
    	node.datano = r;
    	node.name = f[i].name;
    	node.next = null;
    	if (j == 0)
    	ha[i].next = node;
    	else
    	la[i].next = node;
    	la[i] = node;
    	}
    	}
    	}
     
     
    	public static void indexed()
    	{
    	 int r = 1;
    	 int i;
    	 int j;
    	 int k = 0;
    	 index t;
    	 for (i = 0;i < n;i++)
    	 {
     
     
    	ha[i] = new list();
     
    	   for (j = 0;j < f[i].nob;j++)
    	   {
    		while(r<(n+1))
    		{
    			r = a[k];
    	 		k++;
    		}	 
     
    		 GlobalMembersFile_allocation.dele(r);
     
     
     
    		list node = new list();
    		 node.datano = r;
    		 node.name = f[i].name;
    		 node.next = null;
    		 if (j == 0)
    		 ha[i].next = node;
    		 else
    		 la[i].next = node;
    		 la[i] = node;
    	   }
    	 }
    	 t = h.next;
    	 for (i = 0;i < n;i++)
    	 {
    	   t.name = f[i].name;
    	   t.datano = i + 1;
    	   t.link = ha[i];
    	   t = t.next;
    	 }
    	}
     
     
    	public static void contdis()
    	{
    	int i;
    	System.out.print("\n\n\t Filename\t start\t Length\n");
    	for (i = 0;i < n;i++)
    	{
    	System.out.printf("\t\t%s\t",f[i].name);
    	System.out.printf("\t%d\t\t",ha[i].next.datano);
    	System.out.printf("%d\n",f[i].nob);
    	}
    	}
     
     
    	public static void linkdis()
    	{
    	int i;
    	int j;
    	System.out.print("\n\n\t Filename\t start\t End\n");
    	for (i = 0;i < n;i++)
    	{
    	System.out.printf("\t\t%s\t\t",f[i].name);
    	System.out.printf("%d\t",ha[i].next.datano);
    	System.out.printf("%d\n",la[i].datano);
    	}
     
     
    	list t;
    	System.out.print("\n\n INDIVIDUAL FILE LISTING:-");
    	System.out.print("\n\t Filename \t datablocks\n");
     
     
    	for (i = 0;i < n;i++)
    	{
    	System.out.printf("\n%s",f[i].name);
    	for (t = ha[i].next;t != null;t = t.next)
    	{
    	System.out.printf("-->%d",t.datano);
    	}
    	}
    	}
     
     
    	public static void indexdis()
    	{
    	int i;
    	index temp;
    	temp = h.next;
    	System.out.print("\n\t FILE NAME \t BLOCK INDEXED\n");
    	for (i = 0;i < n;i++)
    	{
    	System.out.printf("\t\t%s",f[i].name);
    	System.out.printf("\t\t\t%d\n",temp.datano);
    	temp = temp.next;
    	}
    	list t;
    	System.out.print("\n\n INDEX TABLE LISTING:-");
    	System.out.print("\n FILENAME  INDEXED BLOCK");
    	for (i = 0;i < n;i++)
    	{
    	System.out.printf("\n%s  \n",f[i].name);
    	for (t = ha[i].next;t != null;t = t.next)
    	System.out.printf("\t\t%d\n",t.datano);
    	}
    	}
    static Scanner sc =new Scanner(System.in);
     
    	public static void main(String args[]) 
    	{
     
    	int z;
    	int ch;
    	GlobalMembersFile_allocation.read();
    	GlobalMembersFile_allocation.createfree();
     
     
    	do
    	{
    	System.out.print("\n\tFILE ALLOCATION TECHNIQUES\n1.CONTIGUOUS\n2.LINKED\n3.INDEXED\n4.EXIT\n\tEnter your choice:-");
     
        ch = sc.nextInt();
    	switch (ch)
    	{
     
     
    	case 1:
    	 GlobalMembersFile_allocation.contiguous();
    	 GlobalMembersFile_allocation.contdis();
    	 GlobalMembersFile_allocation.createfree();
    	 break;
     
     
    	case 2:
     
    	 GlobalMembersFile_allocation.linked();
    	 GlobalMembersFile_allocation.linkdis();
    	 GlobalMembersFile_allocation.createfree();
    	 break;
     
     
    	case 3:
     
    	 GlobalMembersFile_allocation.createind();
    	 GlobalMembersFile_allocation.indexed();
    	 GlobalMembersFile_allocation.indexdis();
    	 GlobalMembersFile_allocation.createfree();
    	 break;
     
     
    	case 4:
    	 System.exit(1);
     
    	}
    	}while (ch != 4);
     
    	}
    }
     
     
    public class file
    {
     public String name = new String(new char[20]);
     public int size;
     public int nob;
    }
     
     
    public class index
    {
     public int datano;
     public String name;
     public list link;
     public index next;
    }
     
     
    public class list
    {
     public int datano;
     public String name;
     public list next;
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: java Error: NullPointerException

    Exception in thread "main" java.lang.NullPointerException
    at GlobalMembersFile_allocation.read(GlobalMembersFil e_allocation.java:94)
    There is a variable with a null value on line 94. Look at line 94 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 94 and print out the values of all the variables on that line.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java Error: NullPointerException

    Quote Originally Posted by Norm View Post
    There is a variable with a null value on line 94. Look at line 94 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 94 and print out the values of all the variables on that line.
    Thanks for your suggestion. I've traced the variable that is null. But how can I put a valid value and what value to assign in it. thanks in advance.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: java Error: NullPointerException

    But how can I put a valid value and what value to assign in it.
    What is supposed to be in the variable? You haven't given out any information about it so I can't make any suggestions.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: February 27th, 2013, 06:48 AM
  2. Replies: 1
    Last Post: November 27th, 2012, 10:19 AM
  3. [SOLVED] Error Message: Exception in thread "main" java.lang.NullPointerException etc.
    By coffecupcake in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 15th, 2011, 09:34 AM
  4. [SOLVED] Java run time error: 'java.lang.NullPointerException"
    By ChandanSharma in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 20th, 2011, 11:53 AM
  5. Getting "AWT-EventQueue-0" java.lang.NullPointerException error
    By tryingtoJava in forum AWT / Java Swing
    Replies: 9
    Last Post: September 21st, 2009, 10:46 PM