Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › {SOLVED} Strange java problem
New Posts  All Forums:Forum Nav:

{SOLVED} Strange java problem

post #1 of 14
Thread Starter 
I am making a chat client that will, when completed, support encryption of various kinds (this is the meaning of the project).

I am trying to implement a private chat function which the user calls like this: "/npchat Jake Hi"

It should then find out the clientNumber of the user Jake, and send the message to him. For this, I have created this function:
Code:
     public int getClientNumberFromName(String clientName){ //DOESNT WORK, returns null
                int out = -1;
                for (int i=0; i<=9; i++) {
                        if (t[i] != null) {
                                try {
                                        System.out.println(clientName + " " + t[i].getClientName());
                                        if (t[i].getClientName().equals(clientName)) {
                                                out = i;
                                        }
                                } catch (NullPointerException e){
                                        System.out.println(e.getMessage());
                                }
                        }
                }
                return out;
        }

getClientName():
Code:
     public String getClientName() {
                return this.name;
        }

Sometimes, t(i) will be null, as client threads are null until a client is assigned to that thread, hence if( t(i) != null). However, it appears that it disregards this if and goes ahead with execution anyway; without the try statement it throws a NullPointerException. With the try statement, the server prints "Jake null" if the input is "/npchat Jake hi", and the function returns -1, which it is supposed to do if either the function fails or the user is not found. Also, it doesn't appear to cycle through all clients, since it only returns one output line, not 10 as it should.

Any ideas?
Edited by CritiCal - 5/11/12 at 11:32am
Old Bertha v6.0
(21 items)
 
Odin
(8 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 3930K ASROCK Fatal1ty X79 Champion GIGABYTE GeForce GTX 780Ti WINDFORCE Corsair Vengeance 
Hard DriveHard DriveHard DriveHard Drive
4 x Samsung ProSeries 840 128GB RAID10 Seagate Barracuda Seagate Barracuda Seagate Barracuda 
Optical DriveCoolingOSMonitor
Samsung SH203 DVD writer Corsair H100 Windows 8 x64 BenQ XL2420Z 
MonitorMonitorMonitorKeyboard
BenQ XL2420T BenQ XL2420T ASUS VH242 Corsair STRAFE RGB 
PowerCaseMouseMouse Pad
Corsair AX1200i Corsair Obsidian 800D Cyborg RAT 9 Golden Gaming 
Audio
Corsair Vengeance 2100 
CPUMotherboardGraphicsRAM
Intel Core i7-3930k ASUS P9X79 WS Zotac Geforce GTX 570 Corsair Vengeance 
Hard DriveCoolingOSCase
Seagate Barracuda 500GB Noctua NH-D14 Windows Server 2008 R2 Cooler Master HAF932 Advanced 
  hide details  
Reply
Old Bertha v6.0
(21 items)
 
Odin
(8 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 3930K ASROCK Fatal1ty X79 Champion GIGABYTE GeForce GTX 780Ti WINDFORCE Corsair Vengeance 
Hard DriveHard DriveHard DriveHard Drive
4 x Samsung ProSeries 840 128GB RAID10 Seagate Barracuda Seagate Barracuda Seagate Barracuda 
Optical DriveCoolingOSMonitor
Samsung SH203 DVD writer Corsair H100 Windows 8 x64 BenQ XL2420Z 
MonitorMonitorMonitorKeyboard
BenQ XL2420T BenQ XL2420T ASUS VH242 Corsair STRAFE RGB 
PowerCaseMouseMouse Pad
Corsair AX1200i Corsair Obsidian 800D Cyborg RAT 9 Golden Gaming 
Audio
Corsair Vengeance 2100 
CPUMotherboardGraphicsRAM
Intel Core i7-3930k ASUS P9X79 WS Zotac Geforce GTX 570 Corsair Vengeance 
Hard DriveCoolingOSCase
Seagate Barracuda 500GB Noctua NH-D14 Windows Server 2008 R2 Cooler Master HAF932 Advanced 
  hide details  
Reply
post #2 of 14
maybe t isn't' null but the getClientName function is returning null for the client name.

I have some other questions like what data structure is t[] and also your loop should always be looping 9 times. Can you get the length of t[] before you loop and use that as your max loop variable. If you did that you might not need to check to see if t
!= null
post #3 of 14
Thread Starter 
Quote:
Originally Posted by SweetAndLow View Post

maybe t isn't' null but the getClientName function is returning null for the client name.
I have some other questions like what data structure is t[] and also your loop should always be looping 9 times. Can you get the length of t[] before you loop and use that as your max loop variable. If you did that you might not need to check to see if t
!= null

I would still have to check if t(i) is null. The reason is that t is defined as a fixed-length array, and when a new client connects it is assigned to a free (i) in t.
Code:
static  clientThread t[] = new clientThread[10];
Code:
class clientThread extends Thread{
    
    DataInputStream is = null;
    PrintStream os = null;
    Socket clientSocket = null;       
    clientThread t[]; 
    int clientNumber = 1337;
    String name;

Assigning to first free t:
Code:
                     for(int i=0; i<=9; i++){
                                if(t[i]==null)
                                {
                                        (t[i] = new clientThread(clientSocket,t,i)).start();
                                        break;
                                }
                        }

Also, t is ALWAYS 10, therefore the the for loop is set to iterate 10 times.
Old Bertha v6.0
(21 items)
 
Odin
(8 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 3930K ASROCK Fatal1ty X79 Champion GIGABYTE GeForce GTX 780Ti WINDFORCE Corsair Vengeance 
Hard DriveHard DriveHard DriveHard Drive
4 x Samsung ProSeries 840 128GB RAID10 Seagate Barracuda Seagate Barracuda Seagate Barracuda 
Optical DriveCoolingOSMonitor
Samsung SH203 DVD writer Corsair H100 Windows 8 x64 BenQ XL2420Z 
MonitorMonitorMonitorKeyboard
BenQ XL2420T BenQ XL2420T ASUS VH242 Corsair STRAFE RGB 
PowerCaseMouseMouse Pad
Corsair AX1200i Corsair Obsidian 800D Cyborg RAT 9 Golden Gaming 
Audio
Corsair Vengeance 2100 
CPUMotherboardGraphicsRAM
Intel Core i7-3930k ASUS P9X79 WS Zotac Geforce GTX 570 Corsair Vengeance 
Hard DriveCoolingOSCase
Seagate Barracuda 500GB Noctua NH-D14 Windows Server 2008 R2 Cooler Master HAF932 Advanced 
  hide details  
Reply
Old Bertha v6.0
(21 items)
 
Odin
(8 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 3930K ASROCK Fatal1ty X79 Champion GIGABYTE GeForce GTX 780Ti WINDFORCE Corsair Vengeance 
Hard DriveHard DriveHard DriveHard Drive
4 x Samsung ProSeries 840 128GB RAID10 Seagate Barracuda Seagate Barracuda Seagate Barracuda 
Optical DriveCoolingOSMonitor
Samsung SH203 DVD writer Corsair H100 Windows 8 x64 BenQ XL2420Z 
MonitorMonitorMonitorKeyboard
BenQ XL2420T BenQ XL2420T ASUS VH242 Corsair STRAFE RGB 
PowerCaseMouseMouse Pad
Corsair AX1200i Corsair Obsidian 800D Cyborg RAT 9 Golden Gaming 
Audio
Corsair Vengeance 2100 
CPUMotherboardGraphicsRAM
Intel Core i7-3930k ASUS P9X79 WS Zotac Geforce GTX 570 Corsair Vengeance 
Hard DriveCoolingOSCase
Seagate Barracuda 500GB Noctua NH-D14 Windows Server 2008 R2 Cooler Master HAF932 Advanced 
  hide details  
Reply
post #4 of 14
will it be gpu accelerated?
post #5 of 14
Thread Starter 
I wasn't planning on it, neither do I see the point. A chat server is scarcely a FLOPS-intensive application, and as such would benefit little from GPU acceleration (especially with the AES New Instructions instruction set on Sandy Bridge and Sandy Bridge-E).

However, it would be fun to write a CUDA-chatserver, just because we can.
Old Bertha v6.0
(21 items)
 
Odin
(8 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 3930K ASROCK Fatal1ty X79 Champion GIGABYTE GeForce GTX 780Ti WINDFORCE Corsair Vengeance 
Hard DriveHard DriveHard DriveHard Drive
4 x Samsung ProSeries 840 128GB RAID10 Seagate Barracuda Seagate Barracuda Seagate Barracuda 
Optical DriveCoolingOSMonitor
Samsung SH203 DVD writer Corsair H100 Windows 8 x64 BenQ XL2420Z 
MonitorMonitorMonitorKeyboard
BenQ XL2420T BenQ XL2420T ASUS VH242 Corsair STRAFE RGB 
PowerCaseMouseMouse Pad
Corsair AX1200i Corsair Obsidian 800D Cyborg RAT 9 Golden Gaming 
Audio
Corsair Vengeance 2100 
CPUMotherboardGraphicsRAM
Intel Core i7-3930k ASUS P9X79 WS Zotac Geforce GTX 570 Corsair Vengeance 
Hard DriveCoolingOSCase
Seagate Barracuda 500GB Noctua NH-D14 Windows Server 2008 R2 Cooler Master HAF932 Advanced 
  hide details  
Reply
Old Bertha v6.0
(21 items)
 
Odin
(8 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 3930K ASROCK Fatal1ty X79 Champion GIGABYTE GeForce GTX 780Ti WINDFORCE Corsair Vengeance 
Hard DriveHard DriveHard DriveHard Drive
4 x Samsung ProSeries 840 128GB RAID10 Seagate Barracuda Seagate Barracuda Seagate Barracuda 
Optical DriveCoolingOSMonitor
Samsung SH203 DVD writer Corsair H100 Windows 8 x64 BenQ XL2420Z 
MonitorMonitorMonitorKeyboard
BenQ XL2420T BenQ XL2420T ASUS VH242 Corsair STRAFE RGB 
PowerCaseMouseMouse Pad
Corsair AX1200i Corsair Obsidian 800D Cyborg RAT 9 Golden Gaming 
Audio
Corsair Vengeance 2100 
CPUMotherboardGraphicsRAM
Intel Core i7-3930k ASUS P9X79 WS Zotac Geforce GTX 570 Corsair Vengeance 
Hard DriveCoolingOSCase
Seagate Barracuda 500GB Noctua NH-D14 Windows Server 2008 R2 Cooler Master HAF932 Advanced 
  hide details  
Reply
post #6 of 14
It should work fine but,

Why are you trying to catch the Null Pointer Exception if you've already checked that "t[ i ]" isn't null?

Just use the try...catch on its own without the if condition.
Edited by {Unregistered} - 5/11/12 at 10:55am
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
post #7 of 14
Thread Starter 
Quote:
Originally Posted by {Unregistered} View Post

It should work fine but,
Why are you trying to catch the Null Pointer Exception if you've already checked that t isn't null?

Because it threw a nullPointerException anyway.

It has, however, apparently sorted itself out, and now works. I did not change anything; perhaps the error was on my part when giving the input.
Old Bertha v6.0
(21 items)
 
Odin
(8 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 3930K ASROCK Fatal1ty X79 Champion GIGABYTE GeForce GTX 780Ti WINDFORCE Corsair Vengeance 
Hard DriveHard DriveHard DriveHard Drive
4 x Samsung ProSeries 840 128GB RAID10 Seagate Barracuda Seagate Barracuda Seagate Barracuda 
Optical DriveCoolingOSMonitor
Samsung SH203 DVD writer Corsair H100 Windows 8 x64 BenQ XL2420Z 
MonitorMonitorMonitorKeyboard
BenQ XL2420T BenQ XL2420T ASUS VH242 Corsair STRAFE RGB 
PowerCaseMouseMouse Pad
Corsair AX1200i Corsair Obsidian 800D Cyborg RAT 9 Golden Gaming 
Audio
Corsair Vengeance 2100 
CPUMotherboardGraphicsRAM
Intel Core i7-3930k ASUS P9X79 WS Zotac Geforce GTX 570 Corsair Vengeance 
Hard DriveCoolingOSCase
Seagate Barracuda 500GB Noctua NH-D14 Windows Server 2008 R2 Cooler Master HAF932 Advanced 
  hide details  
Reply
Old Bertha v6.0
(21 items)
 
Odin
(8 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 3930K ASROCK Fatal1ty X79 Champion GIGABYTE GeForce GTX 780Ti WINDFORCE Corsair Vengeance 
Hard DriveHard DriveHard DriveHard Drive
4 x Samsung ProSeries 840 128GB RAID10 Seagate Barracuda Seagate Barracuda Seagate Barracuda 
Optical DriveCoolingOSMonitor
Samsung SH203 DVD writer Corsair H100 Windows 8 x64 BenQ XL2420Z 
MonitorMonitorMonitorKeyboard
BenQ XL2420T BenQ XL2420T ASUS VH242 Corsair STRAFE RGB 
PowerCaseMouseMouse Pad
Corsair AX1200i Corsair Obsidian 800D Cyborg RAT 9 Golden Gaming 
Audio
Corsair Vengeance 2100 
CPUMotherboardGraphicsRAM
Intel Core i7-3930k ASUS P9X79 WS Zotac Geforce GTX 570 Corsair Vengeance 
Hard DriveCoolingOSCase
Seagate Barracuda 500GB Noctua NH-D14 Windows Server 2008 R2 Cooler Master HAF932 Advanced 
  hide details  
Reply
post #8 of 14
Quote:
Originally Posted by CritiCal View Post

Because it threw a nullPointerException anyway.
It has, however, apparently sorted itself out, and now works. I did not change anything; perhaps the error was on my part when giving the input.

Glad you got it sorted out, but you shouldn't need the if condition with the try...catch blocks.
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
post #9 of 14
Thread Starter 
Quote:
Originally Posted by {Unregistered} View Post

Glad you got it sorted out, but you shouldn't need the if condition with the try...catch blocks.

It's gone. That was just because it threw the nullPointerException even with the if. It doesn't do that anymore, though, and therefore it has been removed.
Old Bertha v6.0
(21 items)
 
Odin
(8 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 3930K ASROCK Fatal1ty X79 Champion GIGABYTE GeForce GTX 780Ti WINDFORCE Corsair Vengeance 
Hard DriveHard DriveHard DriveHard Drive
4 x Samsung ProSeries 840 128GB RAID10 Seagate Barracuda Seagate Barracuda Seagate Barracuda 
Optical DriveCoolingOSMonitor
Samsung SH203 DVD writer Corsair H100 Windows 8 x64 BenQ XL2420Z 
MonitorMonitorMonitorKeyboard
BenQ XL2420T BenQ XL2420T ASUS VH242 Corsair STRAFE RGB 
PowerCaseMouseMouse Pad
Corsair AX1200i Corsair Obsidian 800D Cyborg RAT 9 Golden Gaming 
Audio
Corsair Vengeance 2100 
CPUMotherboardGraphicsRAM
Intel Core i7-3930k ASUS P9X79 WS Zotac Geforce GTX 570 Corsair Vengeance 
Hard DriveCoolingOSCase
Seagate Barracuda 500GB Noctua NH-D14 Windows Server 2008 R2 Cooler Master HAF932 Advanced 
  hide details  
Reply
Old Bertha v6.0
(21 items)
 
Odin
(8 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 3930K ASROCK Fatal1ty X79 Champion GIGABYTE GeForce GTX 780Ti WINDFORCE Corsair Vengeance 
Hard DriveHard DriveHard DriveHard Drive
4 x Samsung ProSeries 840 128GB RAID10 Seagate Barracuda Seagate Barracuda Seagate Barracuda 
Optical DriveCoolingOSMonitor
Samsung SH203 DVD writer Corsair H100 Windows 8 x64 BenQ XL2420Z 
MonitorMonitorMonitorKeyboard
BenQ XL2420T BenQ XL2420T ASUS VH242 Corsair STRAFE RGB 
PowerCaseMouseMouse Pad
Corsair AX1200i Corsair Obsidian 800D Cyborg RAT 9 Golden Gaming 
Audio
Corsair Vengeance 2100 
CPUMotherboardGraphicsRAM
Intel Core i7-3930k ASUS P9X79 WS Zotac Geforce GTX 570 Corsair Vengeance 
Hard DriveCoolingOSCase
Seagate Barracuda 500GB Noctua NH-D14 Windows Server 2008 R2 Cooler Master HAF932 Advanced 
  hide details  
Reply
post #10 of 14
This may seem like a dumb question, but are you ever initializing the String name in the clientThread class? I can see you call a constructor:
new clientThread(clientSocket,t,i)
But that doesn't look like it takes a string argument. Just making sure you aren't missing something simple.
i7
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 920 D0 w/ HT On [4.01ghz @ 1.208v] Asus P6X58D Premium Asus HD5870 G.SKILL 6GB (3X2GB) DDR3 @ 1528MHz 
Hard DriveOptical DriveOSMonitor
OCZ Vertex Turbo 60GB FW1.5 2xSamsung Spinpoint F3 LG 8x Blu-ray Combo Drive Windows 7 64-bit Dell U2410 Rev. A04 
PowerCase
OCZ ModXStream Pro 700W V2.2 HAF 922 
  hide details  
Reply
i7
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 920 D0 w/ HT On [4.01ghz @ 1.208v] Asus P6X58D Premium Asus HD5870 G.SKILL 6GB (3X2GB) DDR3 @ 1528MHz 
Hard DriveOptical DriveOSMonitor
OCZ Vertex Turbo 60GB FW1.5 2xSamsung Spinpoint F3 LG 8x Blu-ray Combo Drive Windows 7 64-bit Dell U2410 Rev. A04 
PowerCase
OCZ ModXStream Pro 700W V2.2 HAF 922 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › {SOLVED} Strange java problem