Multi-Threading - Unable To Get Outputs But Many NPEs Error

Feb 14, 2014

I have been trying for days and nights , no compile error , but alot of NPEs error .......

This is my Test program

import java.util.*;
public class Test
{
public static void main (String[]args)
{
int totalShares = 0 ;
double totalCost = 0.0;
double totalProfitLoss = 0.0;

[Code] ....

I am expecting a output like this (Which I have error running:

Tracking of Stock : FCS
8 shares has been brought at $37.61
Total shares now 8 at total cost $300.88
33 shares has been brought at $36.31
Total shares now 41 at total cost $1499.11
17 shares has been sold at $42.67
Total shares now 24 at total cost $773.72
19 shares has been sold at $32.31
Total shares now 5 at total cost $159.83
31 shares has been brought at $33.85
Total shares now 36 at total cost $1209.18
28 shares has been brought at $36.37
Total shares now 64 at total cost $2227.54
20 shares has been brought at $35.49
Total shares now 84 at total cost $2937.34
At $36.00 per share, profit is $86.66

View Replies


ADVERTISEMENT

Calling Multi-Threading With For Loop - Error

Feb 2, 2015

I have a problem with multi threading calling with for loop. I have those two and many other classes in one jar file but n 2 different packages.

When i execute the jar file first runs the Aclass, from where i try to run multi threads. From the run() of the class RunTheGame i call many other classes which are in the same package. Like you can see from the for loop is executed the threads.

The problem is that when the second thread starts the first one is stopped this happens for all the threads, more simply when a new thread starts the old one is dead.

It seems that liken the threads uses the classes called from run() of the class RunTheGame as non multi threaded. It gives this error:

Java Code:

Exception in thread "Thread-0" java.lang.NullPointerException
at thePackage2.EClass.getWhiteRemaining(EClass.java:49)
at thePackage2.EClass.isFinal(DClass.java:84)
at thePackage2.Spiel.<init>(Cclass.java:76)
at thePackage2.RunTheGame.run(RunTheGame.java:198)
at java.lang.Thread.run(Thread.java:745) mh_sh_highlight_all('java');

All the above refereed classes are called from the from run() of the class RunTheGame . Like i understand all the threads use those classes as non thread autonomous classes.

Java Code:

public class Aclass {
private static Thread t[];
public static void main(String[] args) throws IOException {
for (int Game = 0; Game< 10; Game++) {
t[Game] = new Thread(new thePackage2.RunTheGame(aplayer, bplayer, Name, ID , Pmach));
t[Game].start();

[Code] .....

View Replies View Related

Servlets :: Multi Threading Mechanism?

Nov 16, 2014

Is there any specific name for servlet's multi threading mechanism? I mean any technical term for it.

View Replies View Related

Java UDP Sockets And Multi-Threading

Dec 26, 2014

I have been practicing writing java code, my university course is going to cover socket programming and multi-threading.

I am presently just starting to write myself a framework for all multiplayer games I may make in the future, my aim really is simply to practice and understand better and this time, im not using an ide, just sublime text, all new grounds for me, I have a good basic understanding of the subject but I want to be fluent.

import java.net.*;
import java.util.ArrayList;
import java.io.*;
/*SMOOTH THREAD SAFE MULTI CLIENT HANDLING CLASS.
*
*This class will create connection objects when a connection is detected, these connections will run in a separate thread and update an array list in their parent class containing their last sent data

[Code] .....

View Replies View Related

Consumer / Producer Multi Threading Scenario

Jul 9, 2014

Just some questions regarding a Consumer producer program I am not understanding. Here are the two classes Below and my questions are :

1. In the main method for NamedConsumer, we created an instance of the class and call it "consumer" and we start it. Then we create another instance and we give its reference to the same variable. Doesn't this in turn destroy the original reference of the first instance we created which was started? Could we have done

NamedConsumer consumer1 = new NamedConsumer( "One", producer );
new Thread( consumer1 ).start();
NamedConsumer consumer2 = new NamedConsumer( "Two", producer );
new Thread( consumer2 ).start();

2. we called start on two difference instances of the NameConsumer class. Doesn't the "run" method need to be synchronized since two threads are hitting it?

Producer.Java

Java Code: public class Producer implements Runnable
{
static final int MAXQUEUE = 5;
private List<String> messages = new ArrayList<String>();

public void run() {
while ( true ) {
putMessage();
try {
Thread.sleep( 1000 );

[code]...

View Replies View Related

Java Socket - Client-server Communication Stuck With Multi-threading

Feb 6, 2015

Firstly, my code is just a demo of my multiplayer game (2 or more players can play simultaneously) to demonstrate my problem without any extra things. I have successfully implemented peer-to-peer (P2P) communication in my game. Later, I decided to add support for client-server communication (ie a central server which is also a player) in my game. It should be much easier than P2P. Now here is the problem:

Suppose, I have 1 server and some clients (may be 1 or more clients). They all should give the following output:

Starting...
A
B
C
E
F
...
...
Done!

They all give the above output without using multi-thread. But using multi-threading, it gives the above output only when there're 1 server and 1 client. I'm using 2 threads (1 for sending, 1 for receiving) for each Socket. That means, each client has only 2 threads for communication but the server has (2 * totalClients) threads.

I've attached my full test project. So, here I'm only showing my 2 thread classes.

ReceiveThread class:
class ReceiveThread extends Thread {
private ObjectInputStream receiveStream;
private BlockingQueue<Character> queue = new ArrayBlockingQueue<Character>(Common.totalClients);

[Code] ....

Since I've attached my full test project, I'm not showing the other 3 classes. They are ServerMain, ClientMain and Common. If I've 2 clients to be connected, then I get the following output:

Server: (Runs first)

Starting...
A
Client 1 (clientID is 1): (Runs after the server)

Starting...
A
B
B
Client 2 (clientID is 2): (Runs after Client 1)

Starting...
A

They are stuck at there, even no exception. Actually, the server and the clients are stuck at the line try { ch = queue.take(); } if more than 1 client are connected. It seems that all are trying to receive data. But without using the dedicated threads, they all work as expected even for more than 1 client. Why is this behaviour? How to solve it? Note that I have used the same SendThread and ReceiveThread classes by which I have succcessfully implemented P2P communication.

About my attached test project file:

It has 5 small .java files for the classes as stated above. It is currently faulty when using additional threads. You have to change clientID variable for each client (they are described inside). But it works as expected without additional threads. To test it without the additional threads:

Comment " TODO" linesUncomment the single lines just after " TODO" linesComment the additional thread construction lines (4 lines)

Currently, for a workaround, I'm not using the dedicated threads for sending and receiving data only for client-server communication in my multi-player game. But I'm still using these threads for P2P communication. I don't know why it is and how to solve it.This is the attached test project file as described above.

Attached File(s) : Test Project.zip (4.11K)

View Replies View Related

Servlets :: Unable To Resolve Class File Error

Sep 9, 2014

I am stuck with my application. I have jsp, servlet application with jasper report server. When I run the application through eclipse, everything works perfectly. But When I try to run it in Tomcat (without eclipse) it gives me the "Unable to resolve the class file " error. I tried all the solutions I got through the google, but still I am unable to come over it.

View Replies View Related

Enterprise JavaBeans :: Error When Deploying - Unable To Link Class

Mar 25, 2013

Trying to deploy the application with webService project fails with below error

weblogic.j2ee.dd.xml.AnnotationProcessException: [EJB:015001]Unable to link class oracle.apps.scm.productCollaboration.common.businessClasses.businessClassesService.applicationModule.
server.BusinessClassServiceImpl in Jar /scratch/software/mw_local/FMWTOOLS_11.1.1.7.0_GENERIC_121222.1001.2_PATCHES4FA_11.1.1.7.0_PLATFORMS/jcyril/mw_home_standalone/user_projects/domains/fusion_domain/servers/AdminServer/upload/ProductLifecycleManagementApp

[Code] ....

View Replies View Related

Joda Time Threading

Nov 20, 2014

I have an application that uses multiple threads to access logic. I am using joda time to compare dates because joda time is thread safe. Below is the example method I am using. My question is being that joda time is thread safe, does it matter whether or not I am using a static method rather than an instance method?

Java Code:

public static boolean isDateEqual(Date checkedDate, String targetDateString) {
boolean passed = false;
if (checkedDate != null && targetDateString != null && !targetDateString.equals("")) {
DateTime checkedDateTime = new DateTime(checkedDate);
DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/dd/yyyy");
DateTime targetDateTime = fmt.parseDateTime(targetDateString);
if (DateTimeComparator.getDateOnlyInstance().compare(checkedDateTime, targetDateTime) == 0) {
passed = true;
}
}
return passed;
} mh_sh_highlight_all('java');

View Replies View Related

Threading Prevent GUI Unresponsiveness?

Jun 22, 2014

HOW does threading prevent GUI unresponsiveness?

View Replies View Related

Adding Outputs From For Loop

Apr 12, 2015

Here is my program. I have to add the outputs from variable 'done' together. It seems to not work inside or outside the loop.

//A program that will find the last/check digit of a Book ISBN number.

import java.util.*;
public class ISBN {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.println("Enter the ISBN number>");
String isbn = kb.nextLine(); //Even though the ISBN code is a number,

[Code] .....

View Replies View Related

Println Of Variables - Not Getting Right Outputs

May 15, 2014

I'm making an armor class. I also made a test for it to ensure it works, but something seems to be wrong. I'm not getting the expect outputs when I ask it to give me a println of the variables.

This is the armor class:

import java.util.Random;
public class Armor
{
//fields
CharacterRace charRace;
String armorName;
public int cost;
public int armorBonus;
public int armorCheckPenalty;
public int arcaneSpellFailChance;

[Code] ....

And this is the CharacterRace class enum:

public enum CharacterRace
{
HUMAN, ELF, HALFELF, HALFORC, DWARVE, GNOME, HALFLING
}//end enum

And lastly, this is what the armor test prints out:

0
0
0
true
true
true

I never get false (I should sometimes) and those 0s should be

30
30
15

What's wrong with my code?

View Replies View Related

Outputs Not Displaying Correct Math

Oct 14, 2014

I'm having extreme troubles with my outputs not displaying the correct math. I have everything organized how I want it, it's just not giving me the correct answers.

The code is supposed prompt the user to enter an investment amount and an interest rate and display the future investment amount for years 1-30. The formula for this is:

futureInvestmentAmount = investmentAmount * (1 + monthlyInterestRate)^(numberOfYears*12)

Here is my code:

import java.util.Scanner;
public class InvestmentValue {
public static void main(String[] args) {
  // prompt user to enter data
Scanner scan = new Scanner(System.in);
System.out.println("The amount invested: ");

[Code] ....

And I have to use a method to do this as it is what we are learning in class right now. A sample output as of now is:

The amount invested:
1000
Annual interest rate:
9
Years Future Value
11.0E15
21.0E27

[Code] .....

And obviously a future investment amount cannot equal infinity.

View Replies View Related

Adding Characters But Getting Numeric Outputs

Oct 23, 2014

I feel like the program is written correctly; however, the interactions pane in DoctorJava gives me numbers instead of letters. I am happy that the numbers it gives me is consistent, but it still bothers me and I do not know what to debug.

import java.util.Scanner;
public class Monogram {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String firstName, middleName, lastName;
int nameLength=1;

[code]...

View Replies View Related

Initializing Object Outputs Zeroes Instead Of Values Given To It

Oct 30, 2014

public class Check2
{
private int red;
private int green;
private int blue;

public Check2(){
red = 0;
green = 0;
blue = 0;

[Code] .....

And when i uses it in another class named "Check" it returns zeroes :

public class Check
{
public static void main(String[] args) {
Check2 obj = new Check2(125,254,12); // the toString method should return the values i gave it here . but it shows me zeroes instead.
System.out.println("the colors are " + obj.toString());
}
}

Output : the colors are (0,0,0)

View Replies View Related

Number Combinations - Restrict Outputs To Suits Of 3 Numbers

Jul 29, 2014

I want to be able to

(A) Restrict the number of output values per line to 3 numbers,
(B) Remove Duplicate lines, where the same numbers are duplicated just outputted in a different order.
(C) Count the number of outlines
(D) Add a fixed column to the outputs that has an ascending count from 1 upwards

I'm trying to achieve. The code is as follows:

package num.com.t1;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
*
*
*/
public class NumComT1 {
public static void main(String... args) {

[Code] ....

View Replies View Related

String Inputs And Outputs - Printing Information To Console

Mar 22, 2014

Write an application that asks the user to enter his/her first name, last name, birthday, and where you born (all fields type String) and prints their information to the console. Use the techniques discuss in class. The data must be encapsulated. The program must be coded in Notpad++ and compiled in the Command Prompt.

Output should be like this:

Welcome!

What is your first name? Carlos

What is your last name? De La Torre

When is your birthday? 08/12/1979

Where did you born? Puerto Rico

First Name: Carlos

Last Name: De La Torre

Birthday : 08/12/1979

Born in : Puerto Rico

This is what I have so far :

package myinfo;
import java.util.Scanner;

public class MyInfo {
private String name;
private String lastName;
private String birthday;
private String birthPlace;

[Code] ....

View Replies View Related

Allow User To Enter Roman Numeral And Then Outputs Integer Number Value - While Loop

Feb 7, 2015

Write a program called RomanNumeralHelper that allows a user to enter a roman numeral and then outputs the integer number value. Use a Scanner to accept command line input from the user and continually ask for a roman numeral until the user enters Q (or q) to stop. Your solution should NOT use a switch statement.

Here is sample input / output:

Enter a roman numeral [Q | q to quit]: III
>> 3
Enter a roman numeral [Q | q to quit]: IV
>> 4
Enter a roman numeral [Q | q to quit]: V
>> 5
Enter a roman numeral [Q | q to quit]: Q
Good Bye!

This is what I have so far in my code, but I cant get what the user inputs when I want it to output the number.

import java.util.Scanner;
public class RomanNumber4
{
public static void main(String[] args) {
// obtain input from command window
Scanner input = new Scanner(System.in);
 
[Code] ....

View Replies View Related

Server / Multi Clients

Mar 9, 2014

I have made a server/multi Clients where Clients send Double data but my problem is that the clients send this data to Server only wonce, and the server waits again the connection from client. (I have to run the clients manually each time)what i'm looking: the client should always send data to server. like an infinite loop.

View Replies View Related

Multi-Array Of JButtons

May 18, 2014

Java Code:

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Screen implements ActionListener {
public JButton[][] b=new JButton[200][200];

[Code] ....

I am trying to create the A* Algorithm and I REALLY need a 2D array to handle this.

This is the error:

Java Code:

at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162)
at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162)
at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162)
at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162)
at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162) mh_sh_highlight_all('java');

View Replies View Related

Multi Array Of Buttons?

May 18, 2014

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

[Code]....

I am trying to program an A* algorithm and I cannot even get past the buttons. I need a 2D array of buttons to handle this problem but I am getting the following errors.

at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162)
at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162)
at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162)
at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162)

[Code] ....

View Replies View Related

Multi-dimension Array Declaration And Instantiation?

May 17, 2015

Whilst pre-preparing for java certification, one of the online mock exams has slightly confused me by saying my answer was incorrect for multi-dimension array 'declaration and instantiation'.

This is one of the answers i chose - which was marked as incorrect

a)
int[][] array2d = {{123}, {4,5}};

Which looks absolutely fine to me.One of the other answers, which i agree is correct and so does the mock exam is

b)
int [][] array2d = new int[2][2];

View Replies View Related

Multi-User Console Chat Program

Nov 22, 2014

My task is to create multiuser console chat program. During whole day of poking around , i finally stand on such concept:

1. When running , Server part must stay into while(true) loop and create threads on each ServerSocket.accept() call, providing socket object inside thread. i.e.

while (true){
System.out.println("Waiting for client to connect");
Thread t = new Thread(new Runner(servSocket.accept(),listOfClientSockets));
System.out.println("Starting thread");
t.start();
}

2. Client side always stays within while(true) loop and waiting for user input with ScannerInstance.nextLine() method.
As soon as user prints something and hits enter, data being captured from scanner and thrown to socket output stream to the server.

My question is : if all parties (X clients and server) are actually in waiting mode (server is waiting for connections and each client is waiting for user input), who will refresh the screen for each client to draw the messages other parties sent?

At this time , each party see updates only when he hits enter and while loop does next iteration reading data from buffer and displaying on console.

View Replies View Related

User To Input Integer And Then Outputs Both Individual Digits Of Number And Sum Of Digits

Feb 2, 2015

Goal is to: Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits.

First I don't know where I made mistakes here, and the only error it finds right now is that str2 was not initialized and I cannot figure out where/when to initialize it.

import javax.swing.JOptionPane;
public class DigitsAndSum {
public static void main (String[] args) {
String str1;
String str2;
int int1 = 0;
int int2 = 0;

[Code] ....

View Replies View Related

How To Access Element Of Multi Dimensional Int Array Using For Loop

Jul 12, 2014

I have written following code.

I want to print all elements in Array to output here is my code

int[][] arr={{12,12,13},
{14,1223,14}};
for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr.length;j++)
{
System.out.println(arr[i][j]);
}
}

but I am getting following output

12
12
14
1223

how to print all elements of int array?

View Replies View Related

JavaFX 2.0 :: Create List Of Entities - Multi-row Tableview?

Apr 10, 2015

I would like to create list of entities which is populated by a search function with the data coming from our REST webservice. However I would like it to be multi-line, with the first line being details from the entity itself and the second line buttons for options that can be performed.
 
So as an example say my entity is People, the first line would contain columns for first name, last name, gender, DOB, etc. The second line would be buttons for "Edit Person", "Print Person details".   With the standard TableView I can't see anyway to alternate between one row of data and another row of buttons.

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved