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; }