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

Json parser without json library
Use Hibernate Criteria for filtering keys and values in Map
Invalidating cache at request end Spring
While loop keeps running but not printing [duplicate]
Android LEB128 type size
Google map inflate exception
Java replace characters in a string except for the given value in a variable
Server Tomcat v8.0 Server at local host failed to start
How to call String value one class to another class in Java
Jetty 8 QueuedThreadPool thread name
How to append elements to String[][]?
Retrieve a given string by ajax - struts 2 [duplicate]
JAXB Binding each two repeated child into bean
Java how to check which condition is true
Google Sign In for android is not working, with a security warning
A randomized array with no repeating values [duplicate]

Categories

HOME
twitter
arduino-uno
hive
c#-4.0
pycharm
reserved
google-play
session
rsyslog
filtering
blueprintjs
vault
rascal
modelica
nstableview
n-gram
try-catch
evopdf
interop
google-cloud-speech
scichart
excel-2007
google-search-api
pass-by-reference
autosys
dxf
c++-amp
file-rename
procdump
directx-10
xquery-3.0
pim
rotational-matrices
catalog
restlet
unspecified
splice
email-templates
glew
eigenvalue
keydown
netcdf4
skeleton-css-boilerplate
domain-model
drupal-6
ifs
nbconvert
redundancy
mediaelement
google-cdn
nxt
heidisql
instant
libpng
angstrom-linux
bluegiga
oauth2client
wapiti
uid
xna-4.0
tmuxinator
purge
pundit
kcachegrind
android-listview
0xdbe
generic-programming
p2
streambase
tween
xojo
has-many-through
elliptic-curve
mechanize-ruby
hyprlinkr
didselectrowatindexpath
typo3-neos
coverflow
tws
dataservice
batterylevel
mqx
sabredav
limejs
html-editor
itmstransporter
spring-portlet-mvc
assembly-loading
deobfuscation
xmemcached
yetanotherforum
appendto
iphone-web-app
lpeg
ncqrs
web-architecture
ti-dsp
sustainable-pace
defensive-programming
multiple-languages
windows-live-messenger

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