Results 1 to 8 of 8
  1. #1
    gyijhbk is offline Member
    Join Date
    Feb 2012
    Posts
    61
    Rep Power
    0

    Default NullPointException

    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    import javax.swing.JFrame;
    
    public class themain {
    	public	static String dosyrvura="";
        static String text="";
    	static gui grafika =null;
    static	client klient =null;
    static survur tema=null;
        public static boolean iskamkilent= false;
    	public static void changeis(String stojnost){
        themain.klient.write( stojnost);
    	}
    	public static void server(String saob)
    	{
    		System.out.println(saob);
    		if(themain.tema!=null)
    		{
    		themain.tema.write(saob);
    		}}
    	public static void zapusk()
    	{
    		themain.klient=new client();
    		klient.start();
    	}
    
        public static void main(String[] args)
    	{
    		gui grafika = new gui();
    		grafika.setSize(500, 500);
    		grafika.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		grafika.setVisible(true);
    		boolean we4eepusnat=false;
    		Socket gotowo=null;
    		ServerSocket i=null;
    		try{
    		 i= new ServerSocket(5555);
    		}
    		catch(IOException e)
    		{	
    		}
    		client klient=null;
    		//iskamkilent=true;
    	 boolean	imaklient=false;
    	
    		System.out.println(iskamkilent);
    		iskamkilent=true;
    		while(true)
    		{	
    			if(iskamkilent==true)
    		{
    				if(imaklient!=true)
    				{
    
    				imaklient=true;
    				}		
    		}
    				try{
    					
    	gotowo=	i.accept();
    			
    				}
    		catch(IOException e)
    		{
    		}	
    		//		System.out.println(iskamkilent);
    			System.out.println(dosyrvura);	
    			dosyrvura="gergggr";
    			if (we4eepusnat==false)
    			{
     themain.tema= new survur(gotowo);
    	themain.tema.start();
    		we4eepusnat=true;
    			}
    			themain.tema.text=text;
    			System.out.println(text);
    			
    		}
    
    	}
    }
    
    class survur extends Thread{
    	   Socket socket=null;
    	String text="";
    	BufferedReader chetene =null;
    	public survur(Socket socket)
    	{ 	
    		this.socket=socket;
    		
    		while (true){if(socket==null)
    		{
    		System.out.println("socketa naistina e null");
    		}
    		gui.polu4eno2.setText(read(socket));
    	//	 write(text);  
    		}
    		
    	}
    	public void run()
    	{
    	}
    	public String read(Socket socket)
    	{
    		
    		String rezultat="";
    		if (socket==null)
    		{
    			return "Ima problem sys socketa (null socket)aaa";
    		}
    		try{
    		chetene = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    		rezultat=chetene.readLine();
    		}
    		catch(IOException e)
    		{	
    		}
            return rezultat;
    	}
    	
    	public boolean write( String danni)
    	{
    		Socket socket=this.socket;
    		if (socket==null)
    		{
    			return false;
    		}
    		if(socket==null)
    		{
    		System.out.println("socketa naistina e null");
    		}
    		PrintWriter print  =null;
    		try{
    		print= new PrintWriter(socket.getOutputStream(),true);
    		}
    		catch(IOException e){
    			
    			return false;
    			
    		}
    		
    		print.println(danni + "\n");
    		System.out.println("222222");
    		return true;
    	}
    }
    class client extends Thread
    {
    	Socket wryzka=null;
    	public static String dosyrvura ="";
    	public client()
    	{
    	 gui.polu4eno2.setText("frfre");
    	 System.out.println("vfrevre");
    	 String dosyrvura=themain.dosyrvura;
    	
    	}
    	public void run()
    	{
    		String dosyrvura=themain.dosyrvura;
    		
    		try{
    	    wryzka = new Socket("localhost",5555);
    		}
    	catch(IOException e)
    	{
    		System.out.println("ima probelm");
    	}
    		while(true)
    		{
    			
    			write(dosyrvura);
    			 System.out.println(dosyrvura);
    			write("aaaaaaaaa");
    		gui.polu4enoklient2.setText(read(wryzka));
    	//	gui.polu4enoklient2.setText("ddddd");
    		System.out.println("1010101001110101010");
    		}
    	}
    	public String read(Socket socket)
    	{
    		BufferedReader chetene=null;
    		String rezultat="";
    		System.out.println("read");
    		try{
    			chetene =new BufferedReader(new InputStreamReader(socket.getInputStream()));
    		}
    		catch(IOException e  )
    		{
    		return "gre6ka w 4eteneto";	
    		}
    		try{
    		rezultat=chetene.readLine();}
    		catch(IOException ee)
    		{
    			return "gre6ka w 4eteneto";	
    		}
    		return rezultat;
    	}
    	public boolean write(String Danni)
    	{
    	Socket	socket=this.wryzka;
    		PrintWriter zapis=null;
    		try
    		{
    			zapis=new PrintWriter(socket.getOutputStream(),true);
    		}
    		catch(IOException e)
    		{
    			return false;
    		}
    		System.out.println(Danni);
    		zapis.println(Danni+"\n");
    		return true;
    	}
    	}
    Java Code:
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    
    public class gui extends JFrame {
    JButton izprati =null;
    JButton izpratiklient=null;
    JButton server=null;
    JButton iskamklient=null;
    JTextField dosyrvura=null;
    JTextField izprati2=null;
    JLabel polu4eno=null;
    JLabel kilent = null;
    JLabel polu4enoklient =null;
    public static JLabel polu4enoklient2 = null;
    public static JLabel polu4eno2=null;
    	public gui()
    {
    	
    	super("The server program");
    	 setLayout(new GridLayout(5,5));
    	 izpratiklient =new JButton("Izprati syob6tenie do syrvyra");
    	 izprati =new JButton("Izprati syob6tenie");
    	 polu4eno= new JLabel("polu4eno:");
    	 polu4eno2=new JLabel("rezultat");
    	 izprati2 =new JTextField(10);
    	 server= new JButton("new button");
    	 polu4enoklient=new JLabel("Nie polu4ihme:");
    	 polu4enoklient2=new JLabel("lolw");
    	 dosyrvura=new JTextField(10);
    	 iskamklient=new JButton("pusni klient");
    	 kilent = new JLabel("------------------------Klient 4ast---------------------------");
    	 add(izprati);
    	 add(izprati2);
    	 add(polu4eno);
    	 add(polu4eno2);
    	 add(kilent);
    	 add(iskamklient);
    	 add(izpratiklient);
    	 add(kilent);
    	 add(dosyrvura);
    	 //add(server);
    	 add(polu4enoklient);
    	 add(polu4enoklient2);
    	 
    	 iskamklient.addActionListener(
    			 new ActionListener(){
    					 public void actionPerformed(ActionEvent event)
    					 {
    						themain.zapusk();
    					 }});
    	 server.addActionListener(
    			 new ActionListener(){
    					 public void actionPerformed(ActionEvent event)
    					 {
    						
    					 }});
         izprati.addActionListener(
        		 new ActionListener()
        		 {
        		public void	 actionPerformed(ActionEvent event)
        			 {
        	
        			 themain.server(izprati2.getText());
        			 };
        		 })	;
         izpratiklient.addActionListener(
        		 new ActionListener()
        		 {
        			 public void actionPerformed(ActionEvent event){
        				 String dosyrvyra=dosyrvura.getText();
        		// themain.dosyrvura=dosyrvura.getText();
        		 themain.dosyrvura="assdd";
        		 themain.changeis(dosyrvura.getText());
        		 themain.iskamkilent=true;
        				// System.out.println(dosyrvura.getText());	
        			 }
        		 }
        		 );
    }
    }

    Why i get error on line 25(themain.tema.write(saob);)?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: NullPointException

    Give us the full error message plus stack trace, and highlight the line on which it occurs in your code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    gyijhbk is offline Member
    Join Date
    Feb 2012
    Posts
    61
    Rep Power
    0

    Default Re: NullPointException

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at themain.server(themain.java:24)
    at gui$3.actionPerformed(gui.java:70)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

    .......
    public static void changeis(String stojnost){
    themain.klient.write( stojnost);
    }
    public static void server(String saob)
    {
    System.out.println(saob);

    >>>>themain.tema.write(saob);<<<<<
    }
    public static void zap
    ..........

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: NullPointException

    So something on that line is null, 'tema' being the obvious candidate.
    Since the line before this checks for that then all I can say is you aren't running the code you think you are.
    I would suggest putting println()'s in there so you can see what's going on, or to step through in a debugger.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: NullPointException

    @gyijhbk you have a few little mistakes. First:

    Java Code:
    Socket socket=this.socket;
            if (socket==null)
            {
                return false;
            }
            if(socket==null)
            {
            System.out.println("socketa naistina e null");
            }
    You have 2 times same thing "if(socket == null)". You should combine them something like this:

    Java Code:
    Socket socket=this.socket;
            if (socket==null)
            {
                System.out.println("socketa naistina e null");
                return false;
            }
    Second, you have "JLabel klient = null;" but later you make tipos because you write everywhere kilent instead of klient.

    There is a few more similar mistakes like this. And after you fix them you can run your code without errors:

    themain.java
    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    import javax.swing.JFrame;
    
    
    public class themain {
    	public static String dosyrvura = "";
    	static String text="";
    	static gui grafika = null;
    	static client klient = null;
    	static survur tema = null;
    	
    	public static boolean iskamklient = false;
    	public static void changeis(String stojnost) {
    		themain.klient.write(stojnost);
    	}
    	public static void server(String saob) {
    		System.out.println(saob);
    		if(themain.tema != null) {
    			themain.tema.write(saob);
    		}
    	}
    	
    	public static void zapusk() {
    		themain.klient = new client();
    		klient.start();
    	}
    	
    	public static void main(String[] args) {
    		gui grafika = new gui();
    		grafika.setSize(500, 500);
    		grafika.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		grafika.setVisible(true);
    		boolean we4eepusnat = false;
    		Socket gotowo = null;
    		ServerSocket i = null;
    		
    		try {
    			i = new ServerSocket(5555);
    		} catch(IOException e) {
    			e.printStackTrace();
    		}
    		client klient = null;
    		
    		boolean imaklient = false;
    		
    		System.out.println(iskamklient);
    		iskamklient = true;
    		
    		while(true) {
    			if(iskamklient == true) {
    				if(imaklient != true) {
    					imaklient = true;
    				}
    			}
    			
    			try {
    				gotowo = i.accept();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    			
    			System.out.println(dosyrvura);
    			dosyrvura = "gergggr";
    			if(we4eepusnat == false) {
    				themain.tema = new survur(gotowo);
    				themain.tema.start();
    				we4eepusnat = true;
    			}
    			themain.tema.text = text;
    			System.out.println(text);
    			}
    		}
    		
    	}
    
    class survur extends Thread {
    	Socket socket = null;
    	String text = "";
    	BufferedReader chetene = null;
    	
    	public survur(Socket socket) {
    		this.socket = socket;
    		
    		while(true) {
    			if(socket == null)
    				System.out.println("Socketa naistina e null");
    			
    			gui.polu4eno2.setText(read(socket));
    		}
    	}
    	
    	public void run() {}
    	
    	public String read(Socket socket) {
    		String rezultat = "";
    		if(socket == null) {
    			return "Ima problem sys socketa (null socket) aaa";
    		}
    		try {
    			chetene = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    			rezultat = chetene.readLine();
    		} catch(IOException e) {
    			e.printStackTrace();
    		}
    		return rezultat;
    	}
    	
    	public boolean write(String danni) {
    		Socket socket = this.socket;
    		if(socket == null) {
    			System.out.println("Socketa naistina e null");
    			return false;
    		}
    		PrintWriter print = null;
    		
    		try {
    			print = new PrintWriter(socket.getOutputStream(), true);
    		} catch(IOException e) {
    			return false;
    		}
    		
    		print.println(danni + "\n");
    		System.out.println("222222");
    		return true;
    	}
    }
    
    class client extends Thread {
    	Socket wryzka = null;
    	public static String dosyrvura = "";
    	public client() {
    		gui.polu4eno2.setText("frfre");
    		System.out.println("vfrevre");
    		String dosyrvura = themain.dosyrvura;
    	}
    	
    	public void run() {
    		String dosyrvura = themain.dosyrvura;
    		
    		try {
    			wryzka = new Socket("localhost", 5555);
    		} catch(IOException e) {
    			System.out.println("ima probelm");
    		}
    		
    		while(true) {
    			write(dosyrvura);
    			System.out.println(dosyrvura);
    			write("aaaaaaaaa");
    			gui.polu4enoklient2.setText(read(wryzka));
    			System.out.println("1010101010101010");
    		}
    	}
    	
    	public String read(Socket socket) {
    		BufferedReader chetene = null;
    		String rezultat = "";
    		System.out.println("read");
    		
    		try {
    			chetene = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    		} catch(IOException e) {
    			e.printStackTrace();
    		}
    		
    		return rezultat;
    	}
    	
    	public boolean write(String Danni) {
    		Socket socket = this.wryzka;
    		
    		PrintWriter zapis = null;
    		
    		try {
    			zapis = new PrintWriter(socket.getOutputStream(), true);
    		} catch(IOException e) {
    			return false;
    		}
    		System.out.println(Danni);
    		zapis.println(Danni+"\n");
    		return true;
    	}
    }
    gui.java
    Java Code:
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    
    public class gui extends JFrame {
    	JButton izprati = null;
    	JButton izpratiklient = null;
    	JButton server = null;
    	JButton iskamklient = null;
    	JTextField dosyrvura = null;
    	JTextField izprati2 = null;
    	JLabel polu4eno = null;
    	JLabel klient = null;
    	JLabel polu4enoklient = null;
    	
    	public static JLabel polu4enoklient2 = null;
    	public static JLabel polu4eno2 = null;
    	
    	public gui() {
    		super("The server program");
    		
    		setLayout(new GridLayout(5,5));
    		
    		izpratiklient = new JButton("Izprati syobstenie do syrvyra");
    		izprati = new JButton("Izprati syobstenie");
    		polu4eno = new JLabel("Poluceno");
    		polu4eno2 = new JLabel("rezultat");
    		izprati2 = new JTextField(10);
    		server = new JButton("new button");
    		polu4enoklient = new JLabel("Nije polucihme:");
    		polu4enoklient2 = new JLabel("lolw");
    		dosyrvura = new JTextField(10);
    		iskamklient = new JButton("pusni klient");
    		klient = new JLabel("---------Klient cast ------");
    		add(izprati);
    		add(izprati2);
    		add(polu4eno);
    		add(polu4eno2);
    		add(klient);
    		add(iskamklient);
    		add(izpratiklient);
    		add(klient);
    		add(dosyrvura);
    		add(polu4enoklient);
    		add(polu4enoklient2);
    		
    		iskamklient.addActionListener(new ActionListener() {
    			
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				themain.zapusk();
    			}
    		});
    		
    		server.addActionListener(new ActionListener() {
    			
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				
    			}
    		});
    		
    		izprati.addActionListener(new ActionListener() {
    			
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				themain.server(izprati2.getText());
    				
    			}
    		});
    		
    		izpratiklient.addActionListener(new ActionListener() {
    			
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				String dosyrvura1 = dosyrvura.getText();
    				
    				themain.dosyrvura="assdd";
    				themain.changeis(dosyrvura.getText());
    				themain.iskamklient = true;
    			}
    		});
    	}
    }

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: NullPointException

    So I was right.
    They weren't running the code that was posted, as it didn't compile...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    gyijhbk is offline Member
    Join Date
    Feb 2012
    Posts
    61
    Rep Power
    0

    Default Re: NullPointException

    But why themain.tema is still null?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: NullPointException

    Have you made the changes suggested by cselic?
    If so have you also stuck in some debugging to see what the values of everything are (and to check that what you are running is wht you think you are running)?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Properties: NullPointException
    By mickylad in forum Advanced Java
    Replies: 2
    Last Post: 11-12-2011, 04:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •