java


Using Interface in java


I want to implement the Myinterface interface in both the classes i.e, OracleImp and SQLImp . But I am getting an error
'Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Hello.main(Hello.java:34)'**
Here is my code
interface MyInterface{
void connect();
}
class OracleImp implements MyInterface{
public void connect(){
System.out.println("Successfully connected to oracle server");
}
}
class SQLImp implements MyInterface{
public void connect(){
System.out.println("Successfully connected to SQL server");
}
}
public class Hello{
public static void main(String args[]) throws Exception {
Class<?> c = Class.forName(args[0]);
MyInterface obj = (MyInterface)c.newInstance();
obj.connect();
}
}
Before you use Class<?> c = Class.forName(args[0]);, you need have a check to ensure the args[0] is not empty.
Like that :
if(args.length<1){
System.exit(0);
}
else{
Class<?> c = Class.forName(args[0]);
...
}

Related Links

What's wrong with my GUI code? [closed]
Java: stuck in a loop
How to Mock Injected Dependencies
Spring controller not created as singleton
Java Test Client can not find properties file in Java Project
Java: Display ArrayList elements as columns in 2d ArrayList
CSS3PIE without PHP or modifying the .htaccess file
How to read a local properties file from an executable jar?
HTTP “get-request” and receiving audio/mpeg content type
Java + NDK load native library
Android some action when button is pressed
Read data from url and save to strings line by line
Inheriting java constructors
I like to add quotes inside a string gathered from the readLine()
Why static method of parent class is called when subclass has already overridden it?
Swing GUI doesn't show when using thread

Categories

HOME
vbscript
c#-4.0
path-finding
plot
onedrive
objectgears
react-router
adb
rsync
tizen-web-app
amazon-ecs
microservices
webpack-2
installshield
mvvmcross
indesign
directx
multiple-records
pheatmap
amazon-cloudformation
awesome-wm
primary-key
maude-system
spring-xd
commonmark
circuit
serverless-framework
fatal-error
reactcsstransitiongroup
jqwidget
django-cms
jprofiler
swingx
plunker
visjs
ml
tokenize
telerik-reporting
nhapi
applozic
elasticsearch-ruby
create-table
fog
hybridauth
vxworks
x11-forwarding
code-search-engine
az-application-insights
unobtrusive-validation
powershell-dsc
apple-news
rainbowtable
strptime
jquery-nestable
ionicons
jlink
sage-one
dotnetzip
veracode
memory-alignment
time-and-attendance
android-fonts
removechild
mathematica-frontend
hill-climbing
cloudbees
rtbkit
google-places
retina
wireshark-dissector
iis-arr
coveralls
ionic
remobjects
truevault
mesa
aapt
doskey
undefined-reference
jboss-weld
viadeo
farseer
dexterity
unc
propertyeditor
javaspaces
eclipse-templates
gwt-ext
nsobject
javax.script
appender
blitz++
dmx512
routedevent
forums
mirah
compiler-specific
substrings
libs
exchange-server-2003
ti-dsp

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App