الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

خطأ في الكود / ! Exception null pointer

بدأه meary في 31 مارس 2011 · 5 رد · 595 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله وبركاته :) والصلاة والسلام على نبينا محمد واله الطيبين الطاهرين

عضوة جديده ... لديها مشكله في الكود / أتمنى ايضاح خطأ هذا الكود في أقرب فرصه :)

وأعدكم بالمساعده في هذا المنتدى الجميل قدر الإستطاعه :)

الكود :

1. الكلاس الأول ..

          public Emplooyee (String first ,String last, double HoursNumber
        ,double rate )
{
    FirstName = first ; // intinal value for the variable FirstName
    LastName = last  ; //intinal value for the variable LastName
    // initial value for the variable NumberOfHoursWorkedLastWeek
    NumberOfHoursWorkedLastWeek = HoursNumber ;
    HourlyRate = rate ;// initial value for the variable rate
}

   public Emplooyee()
   {

   }

 	        // done with the Construcor
Scanner input = new Scanner( System.in );
   // set methods :

           // Done with set method

   public void SetEmployee()
        {
          Scanner input = new Scanner( System.in );
        System.out.println( "Please enter Employee first name :" );//ask the user to inpute the first name 
               FirstName = input.next();//store whatever the user input 
         System.out.println( "Please enter Employee Last name :" );//ask the user to inpute the last name 
               LastName = input.next();//store whatever the user input
          System.out.println( "Please enter number of hours worked last week by Employee :" );//ask the user to inpute the hours worked last week by Employee 
              NumberOfHoursWorkedLastWeek = input.nextDouble();//store whatever the user input
         System.out.println( "Please enter the Hourly Rate for Employee:" );//ask the user to inpute the Hourly Rate 
              HourlyRate = input.nextDouble ();//store whatever the user input 


        }
                                     // get method


       public    String getFirstName () // get method for the FirstName Variable

            {
           	return FirstName ; // return the value which method set store
            }

        public  String getLastName ()// get method for the LasttName Variable
         {
            	return LastName ; // return the value which method set store
         }

      // get method for the NumberOfHoursWorkedLastWeek Variable
      public   double getNumberOfHoursWorkedLastWeek ()
    {
// eturn the value which method set store
          return NumberOfHoursWorkedLastWeek ;
      }

     public double getHourlyRate () // get method for the HourlyRate Variable
    {

         return HourlyRate ;// rerurn the Hourly Rate input by the user

     }

     public double MonthlySalary ()// function to calculate the Salary
        {
         // if the employee worked extra hours .. he/she will has extra money :D
         if (NumberOfHoursWorkedLastWeek > 40)
// Calculate the Salary
             MonthlySalary = (40*HourlyRate)+ (NumberOfHoursWorkedLastWeek - 40 *HourlyRate*1.5 );
         else
            MonthlySalary = (HourlyRate*NumberOfHoursWorkedLastWeek);

         return MonthlySalary ; // return the salary :)
     }
// Done with the get method


   public void  printEmployee()//function to print out all the info. for the employee :)
  {

   System.out.println(""+FirstName+"|/t/t"); // print the first name
   System.out.println(""+LastName+"|/t/t");// print the last name
   System.out.println(""+NumberOfHoursWorkedLastWeek+"|/t/t");// print the number of hours worked last week
   System.out.println(""+HourlyRate+"|/t/t");// print the Hourly Rate
   System.out.println(""+MonthlySalary+"|/t/t");// print out the Monthly Salary
   System.out.println("-------------------"); // mshan al.jdwl !

   }
       } // done wuth the class

2. الكلاس الثاني

public class EmplooyeeTest { // new class :)

 public static void main(String[]args) 
       {

   Emplooyee Employeelist[] = new Emplooyee[5]; // making an array ... with size 5 and name Employeelist :)

             /* a for loop from one to the size which is 5 to store the info. input by the user for each employee */
           for (int i=0 ; i<Employeelist.length ; i++)

             Employeelist.SetEmployee();// call the SetEmployee method for the object Employeelist " which is an array and the object type is the "CLASS" Employee


     System.out.println( "#| First Name | Last Name | Number of hours worked last week | Hourly Rate | MonthlySalary" );// header for the schedul :)
    // a for loop from one to the size of the array which is 5
       for (int j=0 ; j<Employeelist.length  ; j++)

                Employeelist[j].printEmployee();// call the print function to print all the info input by the user for each employeee


         } // end the main

}// end the class

استخدم برنامج : NetBeans 6.9.1

الخطأ الذي يظهر :

Exception in thread "main" java.lang.NullPointerException

at EmplooyeeTest.main(EmplooyeeTest.java:12)

Java Result: 1

BUILD SUCCESSFUL (total time: 0 seconds)

شكراً جزيلاً مقدماً :) لا عدمناكم !

#2

الكلاس الاولى غير كامله

Software Developer
Mahmoudkelany.com


 

#3

عذراً أخي , خط غير مقصود ..

هذا بداية الكود :

     import java.util.*; // import line for scanner
    public class Emplooyee {

 // declare a variable with the name FirstName and type String ( array of char )
    String FirstName ;
 // declare a variable with the name LastName and type String ( array of char )
    String LastName ;
 // declare a variable with the mame NumberOfHoursWorkedLastWeek and type double
    double NumberOfHoursWorkedLastWeek ;
 // declare a variable with the mame HourlyRate  and type double
    double HourlyRate ;
 // declar a variable with the name ExtraHours with the type double
    double MonthlySalary;
  // declare a variable with the mame MonthlySalary and type double


 // the constructor

 /* the line below I sent 4 variables  to the constructor
  ( and it name is same as the class name )*/
#4

السلام عليكم ...

اتصور ان الخطأ معك يا اخي الكريم في تشكيل مصفوفة من الكائنات ... في الكود لديك انت قمت بتعريف مصفوفة لكائنات من النوع Emplooyee

 Emplooyee Employeelist[] = new Emplooyee[5];

و الخطأ انك بعدها لم تنشأ الكائنات نفسها و لكنك افترضت انها موجودة و قمت بإستدعاء الميثود setEmployee

for (int i=0 ; i<Employeelist.length ; i++)

             Employeelist.SetEmployee();

الصحيح في إنشاء مصفوفة من الكائنات هو ان تنشأ المصفوفة اولاً ثم بعدها تنشأ الكائنات و اخيرا تقوم بإستدعاء الميثودس التي تريدها كما يلي:

for (int i=0 ; i<Employeelist.length ; i++)

             Employeelist = new Emplooyee()  ;

بالتوفيق

تم تعديل هذه المشاركة بواسطة بنت اليمن في 1 أبريل 2011 في 01:08

1

لا اله الا الله .. محمد رسول الله

(* ربِ اجعلني مقيم الصلاة و من ذريتي ، ربنا و تقبل دعـاء *)

يا حيّ يا قيـوم ... برحمتك استغيث ... اصلح لي شأني كله و لا تكلني الى نفسي طرفة عين

#5

انا لدي نفس رأي الاخت بنت اليمن

لان هذا الخطلأ يظهر عادة اوجود نقص بالمتغيرات او الكائنات او المصفوفات التي نحاول تنفيذها داخل الميثود الا انها تكون غير موجودة فعليا

إذا نظــرتَ نيـوب اللـّيـثِ بـارزةً         فـلا تظــنَنَ أَنَّ اللـّيـــثَ يبْتسـِـــمُ


 


 


رسم المخططات البيانية بواسطة الجافا


 


كتاب تحليل وتصميم نظم المعلومات


#6

شكراً جزيلاً أعزائي :)

جمعة مباركة على الجميع إن شاء الله

مواضيع مشابهة