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

[تنبيه - عنوان غير مناسب : ]ماسبب هذا ال Exception ?

بدأه ksu في 11 ديسمبر 2009 · 8 رد · 596 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

يظهر الاستثناء عند الاضافة الى مصفوفة مكونة من Objects وهذا الاوبجيك لديه الطريقة method اضافة insert(String,int,int) 1

public void analysis(){

			while(input.hasNext()){
				// input instance of Scanner;
				line = input.nextLine();
				spline = line.split(" ");
				Line++;
				String me = null;
				String Word= null;
				int t=0;
				for(int i =0;i<spline.length;i++){
					me= spline;
					char [] de= me.toCharArray();
					t=(de.length)-1;
					Word = spline;
						//من هنا الاستثناء يظهر
					rs[t].insert(spline,Line,i);


					}
			}

الطريقة insert من كلاس اخر  


[code] public void insert(String wor,int l,int s){
	 if(search(wor)){
		 // method in the same class;
		 addOccr(wor,l,s);
	 }else{
		 // LinkedList in this class;
		 lls.addFirst(new wordData(wor,l,s));
	 }
 }
#2

الكود الذي وضعت لا يعطي فكرة عن سبب الخطأ، ممكن يكون السبب أن rs[t] تساوي null

ممكن تضع الكود كاملا وأيضا رسالة الخطأ كاملة؟؟

#3

شكرا يا اخي على مطالعتك

هذا الكلاس كامل بحيث يقرأ كل كلمة بالسكانر ويدرج الكلمة في اوبجت معين من المصفوفة

import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.Scanner;
public class Wa {

   Scanner input;

	private String line;
	private int Line;
	private String [] spline;
	arrayList []rs ;

	public Wa(){
		Line=0;
		rs = new arrayList[30];
	}


	public void loadFile(String fName){
		try{
			input = new Scanner(new File(fName));	
				}catch(FileNotFoundException E){
					E.printStackTrace();
					System.exit(1);
				}
	}


	public void analysis(){

			while(input.hasNext()){
				// input instance of Scanner;
				line = input.nextLine();
				spline = line.split(" ");
				Line++;
				String me = null;
				String Word= null;
				int t=0;
				for(int i =0;i<spline.length;i++){
					me= spline;
					char [] de= me.toCharArray();
					t=(de.length)-1;
					Word = spline;
						//من هنا الاستثناء يظهر وهو السطر رقم 46
					rs[t].insert(spline,Line,i);


					}
				}
	}

هذه نسخة من الاوبجت التي تحويه المصفوفة وهو يسمى arrayLiast والتسمية مزاجية اي لاتعني شي من وظيفة الكلاس

import java.util.LinkedList;
public class arrayList {
 LinkedList<wordData> lls = new LinkedList<wordData>();

 public arrayList(String h,int f ,int k){
	 lls.addFirst(new wordData(h,f,k));
 }
 public void add(String f,int l,int p){
	 lls.addFirst(new wordData(f,l,p));
 }
 public boolean search(String s){
	 int size= lls.size();

	 for(int i=0;i<size;i++){
		 if(lls.get(i).getWord().equals(s) ){
			 return true;
		 }else{
			 return false;
		 } 
	 }
	 return false;
 }


 public void addOccr(String w,int lin,int plc){
	 int size= lls.size();
	 lls.getFirst();
	 for(int i=0;i<size;i++){
		 if(lls.get(i).getWord().equals(w) ){
			 lls.get(i).addOccr(lin, plc);
		 }
	 }
 }
 public void insert(String wor,int l,int s){
	 if(search(wor)){
		 // method in the same class;
		 addOccr(wor,l,s);
	 }else{
		 // LinkedList in this class;
		 lls.addFirst(new wordData(wor,l,s));
	 }
 }

رساله الخطأ

Exception in thread "main" java.lang.NullPointerException
	at Wa.analysis(Wa.java:46)
	at test.main(test.java:12)
#4
rs = new arrayList[30];

في هذا السطر تقوم بإنشاء مصفوفة تحتوي على 30 object من فئة arraylist، لكنها كلها تساوي null، يعني لازم تعمل لها instanciation وإلا ستحصل على NullPointerException.

مثلا داخل الloop تضع

rs[t] = new arraylist(spline,Line,i);

في الحقيقة لم أفهم الكود الموجود في الloop، خصوصا الجزء الذي تغير فيه قيمة المتغير t

تم تعديل هذه المشاركة بواسطة herch في 11 ديسمبر 2009 في 19:58

#5
herch كتب:

rs = new arrayList[30];

في هذا السطر تقوم بإنشاء مصفوفة تحتوي على 30 object من فئة arraylist، لكنها كلها تساوي null، يعني لازم تعمل لها instanciation وإلا ستحصل على NullPointerException.

مثلا داخل الloop تضع

rs[t] = new arraylist(spline,Line,i);

في الحقيقة لم أفهم الكود الموجود في الloop، خصوصا الجزء الذي تغير فيه قيمة المتغير t

الف شكر اخي herch وجزاك الله خيرا

الكود الذي يحوي التكرا هو عبارة عن سكانر يقرا من التكست فايل كل سطر ويقسمه الى كلمات وكل كلمة يجب ان تدرج في ستركشر معين

واما ما جاء في قيمة t فأن جعلتها لأعلم طول السترينق كان علي أن استخدم الميثود length في كلاس ستريغ

شكرا مرة اخري اخي herch وانا ممتن لك ودمت بسعد

#6

هل اختفت رسالة الخطا؟؟

#7
 /**
         *  you never use null checkers in your code
         *  you can find where's ur problem this way.
         *  Your code is bad :( not understandable.
         *  Lower case class names are not recommended
         *
         */
        public void analysis(){

            if(input != null)
            {

                while(input.hasNext()){

                    // input instance of Scanner;
                line = input.nextLine();
                System.out.println("Line: " + line);
                if(line != null && line.contains(" "))
                {
                    spline = line.split(" ");
                    Line++;
                    String me = null;
                    String Word= null;
                    int t=0;
                    for(int i =0;i<spline.length;i++){
                            me= spline;
                            char [] de= me.toCharArray();
                            t=(de.length)-1;
                            Word = spline;
                                    //من هنا الاستثناء يظهر وهو السطر رقم 46
                            rs[t].insert(spline,Line,i);


                            }
                }
                else
                {
                    System.out.println("line is null or it does not contain any space: ");
                }
            }
            }
             else
            {
                System.out.println("input is null");
            }
        }
#8

الأخ الكريم/الأخت الكريمة

السلام عليكم ورحمة الله وبركاته

مرحباً بكم في منتدى الفريق العربي للبرمجة

نود تنبيهك أن العنوان غير مناسب.

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

وفي حال التكرار سيتعرض موضوعك للإغلاق والحذف!!!

قواعد المشاركة

/index.php?showtopic=29343

شاكرين لكم حُسن تعاونكم

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#9

اخ herch الحمد لله الرسالة اختفت :cool:

الاخ nullone شكرا على الاضافة ولكن كتابة الشيفرة كانت على السريع فلم اتوفق من تحديد اسامي المتغيرات بفعالية

الاخ علاء الصالحي

انا اكون اخاك الكريم في الاسلام ان شاء الله ونأسف على هذا العنوان ونعدك بالأفضل

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